739. 每日溫度

- 思路
- example
- 一維數(shù)組,要尋找任一個元素的右邊或者左邊第一個比自己大或者小的元素的位置
- 單調(diào)棧
[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
- 單調(diào)下降順序
- 復(fù)雜度. 時間: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
- 沒有重復(fù)元素 的數(shù)組
- 兩個數(shù)組, nums1是nums2子集。在nums2中找到nums1相同元素,然后在nums2中找更大的元素。
nums2: 對每一個元素,找右邊第一個更大元素
hash上面結(jié)果
遍歷 nums1
- 復(fù)雜度. 時間: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