forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountCars.py
More file actions
51 lines (47 loc) · 1.44 KB
/
countCars.py
File metadata and controls
51 lines (47 loc) · 1.44 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
# Codility Problem - countCars
def solution(A):
# Logic: Find the element 0 and pair the zero with all one indexes
# * Only the number of pairs is required
# Dictionary method - wont be feasible as the indexes get messed up
n = len(A)
noz = 0
prev_count = 0
count = 0
for i in range(n):
if prev_count > 1000000000:
return -1
if A[i] == 0:
prev_count = prev_count + noz*count
if prev_count > 1000000000:
return -1
count = 0
noz += 1
else:
count += 1
if i == n-1:
prev_count = prev_count + noz*count
if prev_count > 1000000000:
return -1
## Had 90 percent success rate, as was returning prev_count *noz which became more than 1000000000, look at what you are returning before proceeding
return prev_count
""" Raw logic frame
n = len(A)
zero_check = 0
prev_count = 0
count = 0
noz = 0
for i in range(n):
if A[i] == 0 or i == n-1:
print(count, noz, prev_count)
prev_count = prev_count + noz*count
# Track number of zeros
noz += 1
# Reset count to zero for the new zero
count = 0
# Record the zero hit
zero_check = i
else:
if i > zero_check:
count += 1
return prev_count
"""