파비의 매일매일 공부기록

2023.04.13 Today's Challenge 본문

Problem Solving/LeetCode

2023.04.13 Today's Challenge

fabichoi 2023. 4. 13. 23:45

https://leetcode.com/problems/validate-stack-sequences/

 

Validate Stack Sequences - LeetCode

Can you solve this real interview question? Validate Stack Sequences - Given two integer arrays pushed and popped each with distinct values, return true if this could have been the result of a sequence of push and pop operations on an initially empty stack

leetcode.com

스택으로 푸는줄 알았으나 그리디로 푸는 문제

class Solution:
    def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
        j, st = 0, []

        for p in pushed:
            st.append(p)
            while st and j < len(popped) and st[-1] == popped[j]:
                st.pop()
                j += 1
        
        return j == len(popped)
반응형

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

2023.04.15 Today's Challenge  (0) 2023.04.15
2023.04.14 Today's Challenge  (0) 2023.04.14
2023.04.12 Today's Challenge  (0) 2023.04.12
2023.04.11 Today's Challenge  (0) 2023.04.11
2023.04.10 Today's Challenge  (0) 2023.04.10
Comments