-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
Expand file tree
/
Copy pathnext_greater_element.py
More file actions
28 lines (23 loc) · 777 Bytes
/
next_greater_element.py
File metadata and controls
28 lines (23 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
"""
Next Greater Element
--------------------
Given an array, find the next greater element for each element in the array.
The next greater element for an element x is the first greater element on the
right side of x in the array. If no such element exists, output -1.
"""
def next_greater_elements(arr):
stack = []
result = [-1] * len(arr)
# Traverse from right to left
for i in range(len(arr) - 1, -1, -1):
while stack and stack[-1] <= arr[i]:
stack.pop()
if stack:
result[i] = stack[-1]
stack.append(arr[i])
return result
if __name__ == "__main__":
sample = [4, 5, 2, 25]
print("Input:", sample)
print("Next Greater Elements:", next_greater_elements(sample))
# Output: [5, 25, 25, -1]