946-Validate-Stack-Sequences

Sat 17 May 2025

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

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
    stack = []
    i = 0  # popped's index

    for x in pushed:
      stack.append(x)
      while stack and stack[-1] == popped[i]:
        stack.pop()
        i += 1

    return not stack
new Solution().validateStackSequences()

Score: 5

Category: leetcode