-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeta.py
More file actions
28 lines (24 loc) · 847 Bytes
/
beta.py
File metadata and controls
28 lines (24 loc) · 847 Bytes
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
def solution(A):
frontEdges = []
backEdges = []
result = 0
front_index = 0
back_index = 0
for index in xrange(len(A)):
# record the front edges and back edges for each disc
frontEdges.append(index + A[index])
backEdges.append(index - A[index])
frontEdges.sort()
backEdges.sort()
while front_index < len(A):
while back_index < len(A) and\
backEdges[back_index] <= frontEdges[front_index]:
back_index += 1
# for each front edge look at the num of back edges before it (this
# means all the back edges are intersections of the front edge),
# finally, -1 for the current disc's back edge
result += back_index - front_index - 1
if result > 10000000:
return -1
front_index += 1
return result