Skip to content

Commit 633ed18

Browse files
committed
Added medium problem
1 parent 1720d53 commit 633ed18

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Medium/next-greater-element.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
# NEXT GREATER ELEMENT
3+
4+
# O(N) time and space
5+
def nextGreaterElement(array):
6+
# Write your code here.
7+
result = [-1] * len(array)
8+
stack = [0]
9+
10+
for i in range(2 * len(array)):
11+
index = i % len(array)
12+
if len(stack) > 0 and array[index] <= array[stack[-1]]:
13+
stack.append(index)
14+
continue
15+
while stack and array[stack[-1]] < array[index]:
16+
j = stack.pop()
17+
result[j] = array[index]
18+
stack.append(index)
19+
20+
return result

0 commit comments

Comments
 (0)