Problem Solving/LeetCode
2023.07.20 Today's Challenge
fabichoi
2023. 7. 20. 23:45
https://leetcode.com/problems/asteroid-collision/
Asteroid Collision - LeetCode
Can you solve this real interview question? Asteroid Collision - We are given an array asteroids of integers representing asteroids in a row. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning
leetcode.com
소행성 충돌하는 로직을 구하는 문제
class Solution:
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
st = []
for a in asteroids:
if not st or a > 0:
st.append (a)
else:
while st and st[-1] > 0 and st[-1] < abs(a):
st.pop()
if st and st[-1] == abs(a):
st.pop()
else:
if not st or st[-1] < 0:
st.append(a)
return st
반응형