739. 每日温度
- 思路
- example
- 一维数组,要寻找任一个元素的右边或者左边第一个比自己大或者小的元素的位置
- 单调栈
[73]
[74], 73: 1
[75], 74: 1
[75, 71, 69]
[75, 72], 69:1, 71: 2
[76], 72: 1, 75: 4
[76, 73], end, 73: 0, 76: 0
- 单调下降顺序
- 复杂度. 时间:O(n), 空间: O(n)
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
res = [0 for _ in range(n)]
stack = [0]
for i in range(1, n):
if temperatures[i] <= temperatures[stack[-1]]:
stack.append(i)
else:
while stack and temperatures[i] > temperatures[stack[-1]]:
idx = stack.pop()
res[idx] = i - idx
stack.append(i)
return res
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
res = [0 for _ in range(n)]
stack = []
for i in range(n):
if not stack or temperatures[stack[-1]] >= temperatures[i]:
stack.append(i)
else:
while stack and temperatures[stack[-1]] < temperatures[i]:
idx = stack.pop()
res[idx] = i - idx
stack.append(i)
return res
class Solution:
def dailyTemperatures(self, temperatures: List[int]) -> List[int]:
n = len(temperatures)
stack = [0]
res = [0 for _ in range(n)]
for i in range(1, n):
while stack and temperatures[i] > temperatures[stack[-1]]:
idx = stack.pop()
res[idx] = i - idx
stack.append(i)
return res
496. 下一个更大元素 I
- 思路
- example
- 没有重复元素 的数组
- 两个数组, nums1是nums2子集。在nums2中找到nums1相同元素,然后在nums2中找更大的元素。
nums2: 对每一个元素,找右边第一个更大元素
hash上面结果
遍历 nums1
- 复杂度. 时间:O(m+n), 空间: O(n)
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
m, n = len(nums1), len(nums2)
res = [-1] * m
stack = [0]
hashmap = collections.defaultdict(int)
for j in range(m):
hashmap[nums1[j]] = j
for i in range(1, n):
while stack and nums2[i] > nums2[stack[-1]]:
index = stack.pop()
if nums2[index] in hashmap: # nums1 is only a subset of nums2
res[hashmap[nums2[index]]] = nums2[i]
stack.append(i)
return res
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
m, n = len(nums1), len(nums2)
table = collections.defaultdict(int)
for i in range(m):
table[nums1[i]] = i
stack = [0]
res = [-1 for _ in range(m)]
for j in range(1, n):
while stack and nums2[j] > nums2[stack[-1]]:
idx = stack.pop()
if nums2[idx] in table:
res[table[nums2[idx]]] = nums2[j]
stack.append(j)
return res
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
m, n = len(nums1), len(nums2)
table = collections.defaultdict(int)
for i in range(m):
table[nums1[i]] = i
res = [-1 for _ in range(m)]
stack = []
for j in range(n):
if not stack or nums2[stack[-1]] >= nums2[j]:
stack.append(j)
else:
while stack and nums2[stack[-1]] < nums2[j]:
idx = stack.pop()
if nums2[idx] in table: # !!!
res[table[nums2[idx]]] = nums2[j]
stack.append(j)
return res
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
m, n = len(nums1), len(nums2)
res = [-1 for _ in range(m)]
nxt = collections.defaultdict(int)
stack = [0]
for j in range(1, n):
while stack and nums2[j] > nums2[stack[-1]]:
idx = stack.pop()
nxt[nums2[idx]] = nums2[j]
stack.append(j)
for i in range(m):
res[i] = nxt[nums1[i]] if nxt[nums1[i]] != 0 else -1
return res
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
m, n = len(nums1), len(nums2)
pos = collections.defaultdict(int)
for i in range(n):
pos[nums2[i]] = i
res = [0 for _ in range(m)]
nxt = [-1 for _ in range(n)]
stack = [0]
for i in range(1, n):
while stack and nums2[i] > nums2[stack[-1]]:
idx = stack.pop()
nxt[idx] = nums2[i]
stack.append(i)
for i in range(m):
res[i] = nxt[pos[nums1[i]]]
return res