Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created October 28, 2020 15:17
Show Gist options
  • Save codecakes/c2664ad3c760776955facd1527d4f6b5 to your computer and use it in GitHub Desktop.
Save codecakes/c2664ad3c760776955facd1527d4f6b5 to your computer and use it in GitHub Desktop.
Next Greater Element Using a Stack
from Stack import MyStack
def next_greater_element(lst):
# Write your code here
s = MyStack()
ln = len(lst)
for idx in range(ln-1, -1, -1):
num = lst[idx]
while not s.is_empty() and s.top() <= num:
s.pop()
lst[idx] = s.top() if s.top() else -1
s.push(num)
return lst
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment