파비의 매일매일 공부기록

2023.07.20 Today's Challenge 본문

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
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.07.22 Today's Challenge  (0) 2023.07.22
2023.07.21 Today's Challenge  (0) 2023.07.21
2023.07.19 Today's Challenge  (0) 2023.07.19
2023.07.18 Today's Challenge  (0) 2023.07.18
2023.07.17 Today's Challenge  (0) 2023.07.17
Comments