-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
35 lines (29 loc) · 1020 Bytes
/
sol.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
from typing import List
class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
going_left = []
right_asteroids = []
for asteroid in asteroids:
if asteroid > 0:
going_left.append(asteroid)
continue
right_asteroid_saved = True
while going_left:
first_left = going_left.pop()
if first_left > abs(asteroid):
going_left.append(first_left)
right_asteroid_saved = False
break
elif first_left == abs(asteroid):
right_asteroid_saved = False
break
else:
# left explodes by right
pass
if right_asteroid_saved:
right_asteroids.append(asteroid)
return right_asteroids + going_left
nums = [-2,-1,1,2]
s = Solution()
res = s.asteroidCollision(nums)
print(res)