-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroidCollision.py
35 lines (31 loc) · 1.12 KB
/
asteroidCollision.py
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
class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
sta = []
for int in asteroids:
if not sta or int > 0:
sta.append(int)
else:
while True:
stTop = sta[-1]
if stTop < 0:
sta.append(int)
break
# very tricky part below....not at my level
# advanced level to run a -negative of the
# int so it matches the top of the stack
# very very tricky and i try to solve it
# regularly
elif sta[-1] == -int:
sta.pop()
break
elif sta[-1] > -int:
break
else:
sta.pop()
# if not stack...as if: not in the stack
if not sta:
sta.append(int)
break
return sta
myVar = Solution()
myVar.asteroidCollision([6,5,-5])