forked from mbobesic/algorithms-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfDiscIntersections.py
More file actions
39 lines (27 loc) · 925 Bytes
/
NumberOfDiscIntersections.py
File metadata and controls
39 lines (27 loc) · 925 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
29
30
31
32
33
34
35
36
37
38
# link: https://codility.com/demo/take-sample-test/number_of_disc_intersections
# name: Number Of Disc Intersections
def solution(A):
# write your code in Python 2.6
if len(A)==0:
return 0
ups = [0]*len(A)
downs = [0]*len(A)
for index in xrange(0,len(A)):
current = A[index]
lower = max(index-current,0)
upper = min(len(A)-1, index+current)
ups[lower] +=1
downs[upper] -= 1
existing = [0] * (len(A)+1)
existing[0] = ups[0]
for index in xrange(1,len(A)):
existing[index] = existing[index-1] + ups[index] + downs[index-1]
result = 0
for index in xrange(0, len(ups)):
if ups[index] > 0:
up = ups[index]
diff = existing[index] - up
result += up*(up-1)/2 + up*diff
if result > 10000000:
return -1
return result