Skip to content

Commit 7bb7f9b

Browse files
committed
Runtime: 151 ms (Top 99.29%) | Memory: 19.30 MB (Top 5.84%)
1 parent 94c8760 commit 7bb7f9b

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1+
// Runtime: 151 ms (Top 99.29%) | Memory: 19.30 MB (Top 5.84%)
2+
13
class Solution:
24
def nextGreaterElements(self, nums: List[int]) -> List[int]:
3-
t=len(nums)
4-
nums+=nums
5-
l=[-1]*len(nums)
6-
d={}
7-
stack=[0]
8-
for x in range(1,len(nums)):
9-
#print(x)
10-
if nums[x]>nums[stack[-1]]:
11-
while nums[x]>nums[stack[-1]] :
12-
l[stack[-1]]=nums[x]
13-
stack.pop()
14-
if stack==[]:
15-
break
16-
#print(l)
17-
stack.append(x)
18-
19-
else:
20-
stack.append(x)
21-
return l[:t]
22-
23-
5+
st = []
6+
n = len(nums)
7+
ans = [-1] * n
8+
for i in range(2*n-1, -1, -1):
9+
while st and st[-1] <= nums[i%n]:
10+
st.pop()
11+
if st and i < n:
12+
ans[i] = st[-1]
13+
st.append(nums[i%n])
14+
return ans

0 commit comments

Comments
 (0)