Skip to content

Commit 0ac0d29

Browse files
committed
committed from zkp
1 parent 3d58af9 commit 0ac0d29

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

LeetCode/nextGreaterElements_3.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from collections import deque
2+
class Solution(object):
3+
def nextGreaterElements(self, nums):
4+
"""
5+
:type nums: List[int]
6+
:rtype: List[int]
7+
"""
8+
if not nums:
9+
return []
10+
d = deque()
11+
a = [None]*len(nums)
12+
max_val = max(nums)
13+
for i, j in enumerate(nums):
14+
while d and d[-1][1] < j:
15+
t = d.pop()
16+
a[t[0]] = j
17+
if j != max_val:
18+
d.append([i, j])
19+
else:
20+
a[i] = -1
21+
# print(d)
22+
for i in nums:
23+
if len(d) == 0:
24+
break
25+
while d and d[-1][1] < i:
26+
t = d.pop()
27+
a[t[0]] = i
28+
return a

0 commit comments

Comments
 (0)