Updated: 2024-03-12
1 min read
On This Page
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
n = len(nums)
res = [-1] * n
stk = []
for i in range(n * 2 - 1, -1, -1):
i %= n
while stk and stk[-1] <= nums[i]:
stk.pop()
if stk:
res[i] = stk[-1]
stk.append(nums[i])
return res