-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-NumberOfDiscIntersections.rb
More file actions
63 lines (57 loc) · 1.46 KB
/
4-NumberOfDiscIntersections.rb
File metadata and controls
63 lines (57 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# you can use puts for debugging purposes, e.g.
# puts "this is a debug message"
# first naive solution
# def solution(a)
# intersecting = 0
# for i in (0...a.count)
# for j in (i + 1...a.count)
# if i + a[i] >= j - a[j]
# intersecting += 1
# end
# return -1 if intersecting > 10_000_000
# end
# end
# intersecting
# end
# second solution, doesn't pass the performance test either :)
# def solution(a)
# from = (0...a.count).map do |i|
# [i - a[i], i + a[i]]
# end.sort_by do |e|
# e.first
# end
# intersecting = 0
# for i in (0...from.count)
# for j in (i + 1...from.count)
# if from[i].last >= from[j].first
# intersecting += 1
# return -1 if intersecting > 10_000_000
# else
# break # an optimization, but apparently still O(2)
# end
# end
# end
# intersecting
# end
# correct solution
def solution(a)
points = []
a.each_with_index { |r, y|
points << [y - r, :a] # starts
points << [y + r, :b] # ends
}
points.sort!
intersect = 0
active = 0
points.each { |p|
case p[1]
when :a # starts
intersect += active
return -1 if intersect > 10_000_000
active += 1
when :b # ends
active -= 1
end
}
intersect
end