Skip to content

Latest commit

 

History

History
47 lines (38 loc) · 1.01 KB

Validate-Stack_Sequences.md

File metadata and controls

47 lines (38 loc) · 1.01 KB
tags Link sr-due sr-interval sr-ease
LC_Medium
Stack
Google
2024-10-17
4
270

Problem name

title: Psuedo Solution
collapse: closed
- First point
- Second point

Problem statement

See if pushed and popped array would make sense in a stack.

Concepts questions: Not a problem that you can use pointers to check if somehing exist, you should just use a stack.

Append to stack every pushed number, and check while stack is not empty + the top of stack is equal to next pop, pop it from stack and increment.

Solution (Optimized)

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

        for n in pushed:
            stack.append(n)
            while stack and i < len(popped) and stack[-1] == popped[i]:
                stack.pop()
                i += 1
        return not stack
        

Cue Flashcards 🗃