-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.py
More file actions
21 lines (17 loc) · 836 Bytes
/
Copy pathsolution.py
File metadata and controls
21 lines (17 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def nextGreater(self, arr):
n = len(arr)
result = [-1] * n # Initialize result array with -1
stack = [] # Stack to store indices
# Traverse the array twice to handle circular nature
for i in range(2 * n):
current_index = i % n # Get actual array index
current_element = arr[current_index]
# Pop elements from stack while current element is greater
while stack and arr[stack[-1]] < current_element:
index = stack.pop()
result[index] = current_element # Found next greater element
# Only push during first pass to avoid duplicates
if i < n:
stack.append(current_index)
return result