目录
学习目标
学习内容
739. 每日温度
496.下一个更大元素 I
学习目标
- 739. 每日温度
- 496.下一个更大元素 I
学习内容
739. 每日温度
739. 每日温度 - 力扣(LeetCode)https://leetcode.cn/problems/daily-temperatures/
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
res = [0]*n
stack = []
for i in range(n):
while stack and temperatures[stack[-1]]<temperatures[i]:
e = stack.pop()
res[e] = i-e
stack.append(i)
return res
496.下一个更大元素 I
496. 下一个更大元素 I - 力扣(LeetCode)https://leetcode.cn/problems/next-greater-element-i/
from collections import defaultdict
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
n = len(nums1)
m = len(nums2)
dic = defaultdict()
stack = []
for i in range(m):
while stack and stack[-1]<nums2[i]:
e = stack.pop()
dic[e]=nums2[i]
stack.append(nums2[i])
for i in range(n):
nums1[i]=dic[nums1[i]] if nums1[i] in dic else -1
return nums1