946 Validate Stack Sequences

Simulation(bad idea) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution: def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: s=[] p1=0 p2=0 while p2<len(popped): target=popped[p2] while p1<len(pushed) and (len(s)==0 or s[-1]!=target): s.append(pushed[p1]) p1+=1 if not s or s[-1]!=target: return False while p2<len(popped) and (s and s[-1]==popped[p2]): s.pop() p2+=1 return p1==len(pushed) and p2==len(popped) and len(s)==0 This is my first thought, simulate the push and pop and see if we can go through the whole given pushed and popped...

March 21, 2023 · mimimi