-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path503.py
More file actions
executable file
·36 lines (35 loc) · 1.43 KB
/
Copy path503.py
File metadata and controls
executable file
·36 lines (35 loc) · 1.43 KB
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
29
30
31
32
33
34
35
36
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
# monotonic stack
# store the index!!
mStack = []
res = []
# construct a circle by copy it the end
circle = copy.deepcopy(nums)
circle.extend(nums)
for i in range(len(circle) - 1, -1, -1):
# for the copy nums do not put them in the answer
if (i >= len(nums)):
while (len(mStack) > 0 and circle[i] >= circle[mStack[-1]]):
mStack.pop(-1)
else:
# see the answer of next greater I
# pop out who have smaller elements and far away
while (len(mStack) > 0 and circle[i] >= circle[mStack[-1]]):
mStack.pop(-1)
# no element
if (len(mStack) == 0):
res.append(-1)
else:
if (circle[mStack[-1]] == circle[i] and mStack[-1] - len(nums) == i):
res.append(-1)
else:
# if its in the copy nums, cicrle one
if (mStack[-1] >= len(nums)):
res.append(nums[mStack[-1] - len(nums)])
# normal number
else:
res.append(nums[mStack[-1]])
mStack.append(i)
res.reverse()
return res