diff --git a/Solution/739. Daily Temperatures/739. Daily Temperatures.py b/Solution/739. Daily Temperatures/739. Daily Temperatures.py new file mode 100644 index 0000000..b0ee276 --- /dev/null +++ b/Solution/739. Daily Temperatures/739. Daily Temperatures.py @@ -0,0 +1,12 @@ +class Solution: + def dailyTemperatures(self, temperatures: List[int]) -> List[int]: + stk = [] + n = len(temperatures) + ans = [0] * n + for i in range(n - 1, -1, -1): + while stk and temperatures[stk[-1]] <= temperatures[i]: + stk.pop() + if stk: + ans[i] = stk[-1] - i + stk.append(i) + return ans \ No newline at end of file diff --git a/Solution/739. Daily Temperatures/readme.md b/Solution/739. Daily Temperatures/readme.md new file mode 100644 index 0000000..9da65d9 --- /dev/null +++ b/Solution/739. Daily Temperatures/readme.md @@ -0,0 +1,124 @@ +--- +comments: true +difficulty: Medium +edit_url: Antim +tags: + - Stack + - Array + - Monotonic Stack +--- + + + +# [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures) + +## Description + + + +

Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

+ +

 

+

Example 1:

+
Input: temperatures = [73,74,75,71,69,72,76,73]
+Output: [1,1,4,2,1,1,0,0]
+

Example 2:

+
Input: temperatures = [30,40,50,60]
+Output: [1,1,1,0]
+

Example 3:

+
Input: temperatures = [30,60,90]
+Output: [1,1,0]
+
+

 

+

Constraints:

+ + + + + +## Solutions + + + +### Solution 1: Monotonic Stack + +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. + +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. + +After the traversal, we return the answer array. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{temperatures}$. + + + +#### Python3 + +```python +class Solution: + def dailyTemperatures(self, temperatures: List[int]) -> List[int]: + stk = [] + n = len(temperatures) + ans = [0] * n + for i in range(n - 1, -1, -1): + while stk and temperatures[stk[-1]] <= temperatures[i]: + stk.pop() + if stk: + ans[i] = stk[-1] - i + stk.append(i) + return ans +``` + +#### Java + +```java +class Solution { + public int[] dailyTemperatures(int[] temperatures) { + int n = temperatures.length; + Deque stk = new ArrayDeque<>(); + int[] ans = new int[n]; + for (int i = n - 1; i >= 0; --i) { + while (!stk.isEmpty() && temperatures[stk.peek()] <= temperatures[i]) { + stk.pop(); + } + if (!stk.isEmpty()) { + ans[i] = stk.peek() - i; + } + stk.push(i); + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + vector dailyTemperatures(vector& temperatures) { + int n = temperatures.size(); + stack stk; + vector ans(n); + for (int i = n - 1; ~i; --i) { + while (!stk.empty() && temperatures[stk.top()] <= temperatures[i]) { + stk.pop(); + } + if (!stk.empty()) { + ans[i] = stk.top() - i; + } + stk.push(i); + } + return ans; + } +}; +``` + + + + + + \ No newline at end of file