Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def solution(a)
- start_points = a.each_with_index.map{|x,i| i-x }.sort
- end_points = a.each_with_index.map{|x,i| i+x}.sort
- intersections = 0
- open_intervals = 0
- while !start_points.empty? && !end_points.empty? do
- if end_points.first < start_points.first
- end_points.shift
- open_intervals -= 1
- else
- intersections += open_intervals
- open_intervals += 1
- start_points.shift
- end
- end
- intersections > 10 * 1000 * 1000 ? -1 : intersections
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement