739. 每日温度
单调栈指的是只增加或只减少的stack,相当于一个memo
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
answer = [0] * len(temperatures)
stack = [0]
for i in range(1, len(temperatures)):
while len(stack) != 0 and temperatures[i] > temperatures[stack[-1]]:
answer[stack[-1]] = i - stack[-1]
stack.pop(-1)
stack.append(i)
return answer
496.下一个更大元素 I
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
result = [-1] * len(nums1)
stack = [0]
for i in range(1, len(nums2)):
if nums2[i] <= nums2[stack[-1]]:
stack.append(i)
else:
while len(stack) != 0 and nums2[i] > nums2[stack[-1]]:
if nums2[stack[-1]] in nums1:
index = nums1.index(nums2[stack[-1]])
result[index] = nums2[i]
stack.pop()
stack.append(i)
return result
标签:part01,List,57,随想录,len,int,temperatures,stack,nums2
From: https://www.cnblogs.com/miramira/p/18215133