Next Greater Element II
class Solution:
def nextGreaterElements(self, nums: List[int]) -> List[int]:
stk = []
cache = {}
N = len(nums)
for ix in range(2 * N):
while stk and stk[-1][1] < nums[ix % N]:
cache[stk.pop()[0]] = nums[ix % N]
stk.append((ix % N, nums[ix % N]))
temp = []
for ix in range(N):
temp.append(-1 if ix not in cache else cache[ix])
return temp