## [739\. 每日溫度](https://leetcode-cn.com/problems/daily-temperatures/)
> Medium
#### 思路
要求多少天后溫度比現在高,我們使用雙for爆破,遍歷日期,在遍歷一遍之后的日期,進行比較。TLE!
#### 代碼(TLE)
python3
```
class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
result = [0] * len(T)
for i in range(len(T)):
for j in range(i,len(T)):
if T[j] > T[i]:
result[i] = j - i
break
return result
```
**更新思路**
學習一波大佬們的解題,獲取新知識:**單調棧**
[李威威](https://leetcode-cn.com/u/liweiwei1419/)大神的回答是這樣的:
```
分析這一類問題的思路是:
1.使用暴力解法
2.分析出過程中數據是先進后出的,所以使用棧
3.分析出為什么要維護棧中數據的單調性
4.棧內一般也會同時保存下標
5.多做題鞏固
```
一般問題問左邊第一個大的小的,右邊第一個大的小的,我們可以想一下處是否能用單調棧求解。
回到這道題,用暴力解法我們看到遍歷的時候數據先是暫存的,當遇到一個比自己大的數字是,這個結果就確認了,然后可以往下遍歷。我們可以使用`遞減棧`來存放溫度和下標,當一個新來的溫度比當前棧頂數據大時。出棧,并記錄當前的下標之差,即過去天數多少錢,溫度比當前出棧的大
如下,吳師兄解題中的動畫。非常清晰,一目了然。
[https://leetcode-cn.com/problems/daily-temperatures/solution/leetcode-tu-jie-739mei-ri-wen-du-by-misterbooo/](https://leetcode-cn.com/problems/daily-temperatures/solution/leetcode-tu-jie-739mei-ri-wen-du-by-misterbooo/)
使用單調棧求解,AC!
#### 代碼
python3
```
class Solution:
def dailyTemperatures(self, T: List[int]) -> List[int]:
stack = []
result = [0] * len(T)
for i,t in enumerate(T):
while len(stack) and t > stack[-1][1]:
result[stack[-1][0]] = i - stack[-1][0]
stack.pop()
stack.append([i,t])
return result
```
- 目錄
- excel-sheet-column-number
- divide-two-integers
- house-robber
- fraction-to-recurring-decimal
- profile
- kids-with-the-greatest-number-of-candies
- qiu-12n-lcof
- new-21-game
- product-of-array-except-self
- minimum-depth-of-binary-tree
- univalued-binary-tree
- shun-shi-zhen-da-yin-ju-zhen-lcof
- permutations
- satisfiability-of-equality-equations
- word-ladder-ii
- ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof
- palindrome-number
- network-delay-time
- daily-temperatures
- longest-common-prefix
- sum-of-mutated-array-closest-to-target
- 周賽專題
- make-two-arrays-equal-by-reversing-sub-arrays
- check-if-a-string-contains-all-binary-codes-of-size-k
- course-schedule-iv
- cherry-pickup-ii
- maximum-product-of-two-elements-in-an-array
- maximum-area-of-a-piece-of-cake-after-horizontal-and-vertical-cuts
- reorder-routes-to-make-all-paths-lead-to-the-city-zero
- probability-of-a-two-boxes-having-the-same-number-of-distinct-balls
- shuffle-the-array
- the-k-strongest-values-in-an-array
- design-browser-history
- paint-house-iii
- final-prices-with-a-special-discount-in-a-shop