Next Greater Element II LeetCode solution in Python

1
class Solution:
2
def nextGreaterElements(self, nums: List[int]) -> List[int]:
3
st = []
4
n = len(nums)
5
ans = [-1] * n
6
for i in range(2 * n - 1, -1, -1):
7
while st and st[-1] <= nums[i % n]:
8
st.pop()
9
if st and i < n:
10
ans[i] = st[-1]
11
st.append(nums[i % n])
12
return ans

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0