From 7cf484c1d1a15bf936aa081f260eddb7a116af2a Mon Sep 17 00:00:00 2001 From: Simon Chu Date: Wed, 23 Apr 2025 23:45:35 +0800 Subject: [PATCH] :sparkles: (Solution): Problem 739. Daily Temperatures Add solution of 739. Daily Temperatures --- solutions/739. Daily Temperatures/README.md | 80 +++++++++++++++++++ .../739. Daily Temperatures/Solution.java | 17 ++++ solutions/739. Daily Temperatures/Solution.py | 10 +++ 3 files changed, 107 insertions(+) create mode 100644 solutions/739. Daily Temperatures/README.md create mode 100644 solutions/739. Daily Temperatures/Solution.java create mode 100644 solutions/739. Daily Temperatures/Solution.py diff --git a/solutions/739. Daily Temperatures/README.md b/solutions/739. Daily Temperatures/README.md new file mode 100644 index 0000000..996626f --- /dev/null +++ b/solutions/739. Daily Temperatures/README.md @@ -0,0 +1,80 @@ +--- +comments: true +difficulty: medium +# Follow `Topics` tags +tags: + - Array + - Stack + - Monotonic Stack +--- + +# [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures/description/) + +## Description + +You're given an array of integers `temperatures`, where each element represents the temperature of a specific day. +Return a new array `answer` such that `answer[i]` indicates how many days you must wait after the i-th day to experience a warmer temperature. +If there’s no future day with a warmer temperature, set `answer[i]` to `0`. + +**Example 1:** +``` +Input: temperatures = [71,73,77,61,73,75,73] +Output: [1,1,3,1,1,0,0] +``` + +**Example 2:** +``` +Input: temperatures = [10,20,30] +Output: [1,1,0] +``` + +**Constraints:** + +* `1 <= n <= 8` + +## Solution + +We use a monotonic stack to track temperatures: +1. Stack stores indices of temperatures in decreasing order +2. When we find a warmer temperature, we pop indices from stack and calculate waiting days +3. The waiting days are the difference between current index and popped index +4. Any temperatures without a warmer day ahead remain in stack (result stays 0) + +```java +class Solution { + public int[] dailyTemperatures(int[] temperatures) { + Stack stack = new Stack<>(); + int[] result = new int[temperatures.length]; + for (int i = 0; i < temperatures.length; i++) { + while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) { + int index = stack.pop(); + result[index] = i - index; + } + stack.push(i); + } + + return result; + } +} +``` + +```python +class Solution: + def dailyTemperatures(self, temperatures: list[int]) -> list[int]: + stack = [] + res = [0] * len(temperatures) + for i, t in enumerate(temperatures): + while stack and t > temperatures[stack[-1]]: + idx = stack.pop() + res[idx] = i - idx + stack.append(i) + return res +``` + +## Complexity + +- Time complexity: $$O(n)$$ + + +- Space complexity: $$O(n)$$ + diff --git a/solutions/739. Daily Temperatures/Solution.java b/solutions/739. Daily Temperatures/Solution.java new file mode 100644 index 0000000..d3f9f14 --- /dev/null +++ b/solutions/739. Daily Temperatures/Solution.java @@ -0,0 +1,17 @@ +import java.util.*; + +class Solution { + public int[] dailyTemperatures(int[] temperatures) { + Stack stack = new Stack<>(); + int[] result = new int[temperatures.length]; + for (int i = 0; i < temperatures.length; i++) { + while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) { + int index = stack.pop(); + result[index] = i - index; + } + stack.push(i); + } + + return result; + } +} \ No newline at end of file diff --git a/solutions/739. Daily Temperatures/Solution.py b/solutions/739. Daily Temperatures/Solution.py new file mode 100644 index 0000000..b3500f3 --- /dev/null +++ b/solutions/739. Daily Temperatures/Solution.py @@ -0,0 +1,10 @@ +class Solution: + def dailyTemperatures(self, temperatures: list[int]) -> list[int]: + stack = [] + res = [0] * len(temperatures) + for i, t in enumerate(temperatures): + while stack and t > temperatures[stack[-1]]: + idx = stack.pop() + res[idx] = i - idx + stack.append(i) + return res