|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Medium |
| 4 | +edit_url: Antim |
| 5 | +tags: |
| 6 | + - Stack |
| 7 | + - Array |
| 8 | + - Monotonic Stack |
| 9 | +--- |
| 10 | + |
| 11 | +<!-- problem:start --> |
| 12 | + |
| 13 | +# [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures) |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Given an array of integers <code>temperatures</code> represents the daily temperatures, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is the number of days you have to wait after the</em> <code>i<sup>th</sup></code> <em>day to get a warmer temperature</em>. If there is no future day for which this is possible, keep <code>answer[i] == 0</code> instead.</p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | +<pre><strong>Input:</strong> temperatures = [73,74,75,71,69,72,76,73] |
| 24 | +<strong>Output:</strong> [1,1,4,2,1,1,0,0] |
| 25 | +</pre><p><strong class="example">Example 2:</strong></p> |
| 26 | +<pre><strong>Input:</strong> temperatures = [30,40,50,60] |
| 27 | +<strong>Output:</strong> [1,1,1,0] |
| 28 | +</pre><p><strong class="example">Example 3:</strong></p> |
| 29 | +<pre><strong>Input:</strong> temperatures = [30,60,90] |
| 30 | +<strong>Output:</strong> [1,1,0] |
| 31 | +</pre> |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>1 <= temperatures.length <= 10<sup>5</sup></code></li> |
| 37 | + <li><code>30 <= temperatures[i] <= 100</code></li> |
| 38 | +</ul> |
| 39 | + |
| 40 | +<!-- description:end --> |
| 41 | + |
| 42 | +## Solutions |
| 43 | + |
| 44 | +<!-- solution:start --> |
| 45 | + |
| 46 | +### Solution 1: Monotonic Stack |
| 47 | + |
| 48 | +This problem requires us to find the position of the first element greater than each element to its right, which is a typical application scenario for a monotonic stack. |
| 49 | + |
| 50 | +We traverse the array $\textit{temperatures}$ from right to left, maintaining a stack $\textit{stk}$ that is monotonically increasing from top to bottom in terms of temperature. The stack stores the indices of the array elements. For each element $\textit{temperatures}[i]$, we continuously compare it with the top element of the stack. If the temperature corresponding to the top element of the stack is less than or equal to $\textit{temperatures}[i]$, we pop the top element of the stack in a loop until the stack is empty or the temperature corresponding to the top element of the stack is greater than $\textit{temperatures}[i]$. At this point, the top element of the stack is the first element greater than $\textit{temperatures}[i]$ to its right, and the distance is $\textit{stk.top()} - i$. We update the answer array accordingly. Then we push $\textit{temperatures}[i]$ onto the stack and continue traversing. |
| 51 | + |
| 52 | +After the traversal, we return the answer array. |
| 53 | + |
| 54 | +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{temperatures}$. |
| 55 | + |
| 56 | +<!-- tabs:start --> |
| 57 | + |
| 58 | +#### Python3 |
| 59 | + |
| 60 | +```python |
| 61 | +class Solution: |
| 62 | + def dailyTemperatures(self, temperatures: List[int]) -> List[int]: |
| 63 | + stk = [] |
| 64 | + n = len(temperatures) |
| 65 | + ans = [0] * n |
| 66 | + for i in range(n - 1, -1, -1): |
| 67 | + while stk and temperatures[stk[-1]] <= temperatures[i]: |
| 68 | + stk.pop() |
| 69 | + if stk: |
| 70 | + ans[i] = stk[-1] - i |
| 71 | + stk.append(i) |
| 72 | + return ans |
| 73 | +``` |
| 74 | + |
| 75 | +#### Java |
| 76 | + |
| 77 | +```java |
| 78 | +class Solution { |
| 79 | + public int[] dailyTemperatures(int[] temperatures) { |
| 80 | + int n = temperatures.length; |
| 81 | + Deque<Integer> stk = new ArrayDeque<>(); |
| 82 | + int[] ans = new int[n]; |
| 83 | + for (int i = n - 1; i >= 0; --i) { |
| 84 | + while (!stk.isEmpty() && temperatures[stk.peek()] <= temperatures[i]) { |
| 85 | + stk.pop(); |
| 86 | + } |
| 87 | + if (!stk.isEmpty()) { |
| 88 | + ans[i] = stk.peek() - i; |
| 89 | + } |
| 90 | + stk.push(i); |
| 91 | + } |
| 92 | + return ans; |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +#### C++ |
| 98 | + |
| 99 | +```cpp |
| 100 | +class Solution { |
| 101 | +public: |
| 102 | + vector<int> dailyTemperatures(vector<int>& temperatures) { |
| 103 | + int n = temperatures.size(); |
| 104 | + stack<int> stk; |
| 105 | + vector<int> ans(n); |
| 106 | + for (int i = n - 1; ~i; --i) { |
| 107 | + while (!stk.empty() && temperatures[stk.top()] <= temperatures[i]) { |
| 108 | + stk.pop(); |
| 109 | + } |
| 110 | + if (!stk.empty()) { |
| 111 | + ans[i] = stk.top() - i; |
| 112 | + } |
| 113 | + stk.push(i); |
| 114 | + } |
| 115 | + return ans; |
| 116 | + } |
| 117 | +}; |
| 118 | +``` |
| 119 | +
|
| 120 | +<!-- tabs:end --> |
| 121 | +
|
| 122 | +<!-- solution:end --> |
| 123 | +
|
| 124 | +<!-- problem:end --> |
0 commit comments