Untitled
Anonymous
plain_text
12/01/2024 4:50 PM
1.1 KB
14
Indexable
from collections import deque
class Solution:
# @param A : list of integers
# @return a list of integers
def solve(self, A):
n = len(A)
st = deque()
temp_stack= deque()
for i in range(len(A)):
# st_A.append(A[i])
# while st_A:
# temp = st_A.pop() # Get current element
# temp_stack = deque() # To store elements temporarily
if len(st)>0:
while st and st[-1] > A[i]:
temp_stack.append(st.pop())
st.append(A[i])
while temp_stack: # Move back elements
st.append(temp_stack.pop())
arr = []
while len(st)>0:
arr.append(st.pop())
arr.reverse()
return arr
Editor is loading...
Leave a Comment