Created
October 28, 2020 15:17
-
-
Save codecakes/c2664ad3c760776955facd1527d4f6b5 to your computer and use it in GitHub Desktop.
Next Greater Element Using a Stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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