forked from Srinivas11789/AlgorithmNuggets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcan.py
More file actions
55 lines (37 loc) · 1.53 KB
/
can.py
File metadata and controls
55 lines (37 loc) · 1.53 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
class Solution(object):
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
# Going through too much confusion with respect to the logic. Be clear and innovative. Confused with indices and flowerbed entries.
# Flowerbed length
l = len(flowerbed)
# While loop for more control
i = 0
# Result array for indices that are possible, could also use count here.
indexes = []
# Control loop
while i < l:
# If one then skip twice to go across the alternative
if flowerbed[i] == 1:
# Check for previous entry already existing and delete if it does
if i-1 in indexes:
del indexes[len(indexes)-1]
i += 2
else:
# Check if previous is zero before appending to indexes
if i-1 > 0 and i-1 not in indexes:
indexes.append(i)
# Check for i = 0 condition
elif i == 0:
indexes.append(i)
i += 1
print indexes
# Compare for possibility
if n <= len(indexes):
return True
else:
return False
# Any Alternative Approach