Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.50 KB | None | 0 0
  1. def solution(a)
  2.   start_points = a.each_with_index.map{|x,i| i-x }.sort
  3.   end_points = a.each_with_index.map{|x,i| i+x}.sort
  4.  
  5.   intersections = 0
  6.   open_intervals = 0
  7.  
  8.   while !start_points.empty? && !end_points.empty? do
  9.     if end_points.first < start_points.first
  10.       end_points.shift
  11.       open_intervals -= 1
  12.     else
  13.       intersections += open_intervals
  14.       open_intervals += 1
  15.       start_points.shift
  16.     end
  17.   end
  18.  
  19.   intersections > 10 * 1000 * 1000 ? -1 : intersections
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement