diff --git a/solution/0700-0799/0739.Daily Temperatures/README.md b/solution/0700-0799/0739.Daily Temperatures/README.md index 6c609aa713a4b..5ffa181726f76 100644 --- a/solution/0700-0799/0739.Daily Temperatures/README.md +++ b/solution/0700-0799/0739.Daily Temperatures/README.md @@ -59,21 +59,13 @@ tags: ### 方法一:单调栈 -单调栈常见模型:找出每个数左/右边**离它最近的**且**比它大/小的数**。模板: +本题需要我们找出每个元素右边第一个比它大的元素的位置,这是一个典型的单调栈应用场景。 -```python -stk = [] -for i in range(n): - while stk and check(stk[-1], i): - stk.pop() - stk.append(i) -``` +我们从右往左遍历数组 $\textit{temperatures}$,维护一个从栈顶到栈底温度单调递增的栈 $\textit{stk}$,栈中存储的是数组元素的下标。对于每个元素 $\textit{temperatures}[i]$,我们不断将其与栈顶元素进行比较,如果栈顶元素对应的温度小于等于 $\textit{temperatures}[i]$,那么循环将栈顶元素弹出,直到栈为空或者栈顶元素对应的温度大于 $\textit{temperatures}[i]$。此时,栈顶元素就是右边第一个比 $\textit{temperatures}[i]$ 大的元素,距离为 $\textit{stk.top()} - i$,我们更新答案数组。然后将 $\textit{temperatures}[i]$ 入栈,继续遍历。 -对于本题,我们需要找出每个数右边**离它最近的**且**比它大的数**,因此我们可以从右往左遍历数组,且需要维护一个从栈底到栈顶单调递减的栈。 +遍历结束后,返回答案数组即可。 -对于当前遍历到的数 `temperatures[i]`,如果栈顶元素 `temperatures[stk[-1]]` 小于等于 `temperatures[i]`,则弹出栈顶元素,直到栈为空或者栈顶元素大于 `temperatures[i]`。如果此时栈不为空,那么栈顶元素就是 `temperatures[i]` 右边离它最近的且比它大的数,更新 `ans[i] = stk[-1] - i`。接着,我们将 $i$ 入栈,继续遍历下一个数。 - -时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `temperatures` 数组的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{temperatures}$ 的长度。 @@ -82,12 +74,14 @@ for i in range(n): ```python class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: - ans = [0] * len(temperatures) stk = [] - for i, t in enumerate(temperatures): - while stk and temperatures[stk[-1]] < t: - j = stk.pop() - ans[j] = i - j + 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 ``` @@ -98,12 +92,14 @@ class Solution: class Solution { public int[] dailyTemperatures(int[] temperatures) { int n = temperatures.length; - int[] ans = new int[n]; Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - while (!stk.isEmpty() && temperatures[stk.peek()] < temperatures[i]) { - int j = stk.pop(); - ans[j] = i - j; + 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); } @@ -119,13 +115,15 @@ class Solution { public: vector dailyTemperatures(vector& temperatures) { int n = temperatures.size(); - vector ans(n); stack stk; - for (int i = 0; i < n; ++i) { - while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) { - ans[stk.top()] = i - stk.top(); + 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; @@ -137,14 +135,16 @@ public: ```go func dailyTemperatures(temperatures []int) []int { - ans := make([]int, len(temperatures)) - var stk []int - for i, t := range temperatures { - for len(stk) > 0 && temperatures[stk[len(stk)-1]] < t { - j := stk[len(stk)-1] - ans[j] = i - j + n := len(temperatures) + ans := make([]int, n) + stk := []int{} + for i := n - 1; i >= 0; i-- { + for len(stk) > 0 && temperatures[stk[len(stk)-1]] <= temperatures[i] { stk = stk[:len(stk)-1] } + if len(stk) > 0 { + ans[i] = stk[len(stk)-1] - i + } stk = append(stk, i) } return ans @@ -156,14 +156,14 @@ func dailyTemperatures(temperatures []int) []int { ```ts function dailyTemperatures(temperatures: number[]): number[] { const n = temperatures.length; - const ans = new Array(n).fill(0); + const ans: number[] = Array(n).fill(0); const stk: number[] = []; - for (let i = n - 1; i >= 0; --i) { - while (stk.length && temperatures[stk[stk.length - 1]] <= temperatures[i]) { + for (let i = n - 1; ~i; --i) { + while (stk.length && temperatures[stk.at(-1)!] <= temperatures[i]) { stk.pop(); } if (stk.length) { - ans[i] = stk[stk.length - 1] - i; + ans[i] = stk.at(-1)! - i; } stk.push(i); } @@ -191,6 +191,34 @@ impl Solution { } ``` +#### Rust + +```rust +impl Solution { + pub fn daily_temperatures(temperatures: Vec) -> Vec { + let n = temperatures.len(); + let mut stk: Vec = Vec::new(); + let mut ans = vec![0; n]; + + for i in (0..n).rev() { + while let Some(&top) = stk.last() { + if temperatures[top] <= temperatures[i] { + stk.pop(); + } else { + break; + } + } + if let Some(&top) = stk.last() { + ans[i] = (top - i) as i32; + } + stk.push(i); + } + + ans + } +} +``` + #### JavaScript ```js @@ -200,14 +228,14 @@ impl Solution { */ var dailyTemperatures = function (temperatures) { const n = temperatures.length; - const ans = new Array(n).fill(0); + const ans = Array(n).fill(0); const stk = []; - for (let i = n - 1; i >= 0; --i) { - while (stk.length && temperatures[stk[stk.length - 1]] <= temperatures[i]) { + for (let i = n - 1; ~i; --i) { + while (stk.length && temperatures[stk.at(-1)] <= temperatures[i]) { stk.pop(); } if (stk.length) { - ans[i] = stk[stk.length - 1] - i; + ans[i] = stk.at(-1) - i; } stk.push(i); } @@ -219,92 +247,4 @@ var dailyTemperatures = function (temperatures) { - - -### 方法二 - - - -#### Python3 - -```python -class Solution: - def dailyTemperatures(self, temperatures: List[int]) -> List[int]: - n = len(temperatures) - stk = [] - 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(); - vector ans(n); - stack stk; - 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; - } -}; -``` - -#### Go - -```go -func dailyTemperatures(temperatures []int) []int { - n := len(temperatures) - ans := make([]int, n) - var stk []int - for i := n - 1; i >= 0; i-- { - for len(stk) > 0 && temperatures[stk[len(stk)-1]] <= temperatures[i] { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - ans[i] = stk[len(stk)-1] - i - } - stk = append(stk, i) - } - return ans -} -``` - - - - - diff --git a/solution/0700-0799/0739.Daily Temperatures/README_EN.md b/solution/0700-0799/0739.Daily Temperatures/README_EN.md index c6cea8007e95e..8737d114cd41e 100644 --- a/solution/0700-0799/0739.Daily Temperatures/README_EN.md +++ b/solution/0700-0799/0739.Daily Temperatures/README_EN.md @@ -45,7 +45,15 @@ tags: -### Solution 1 +### 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}$. @@ -54,12 +62,14 @@ tags: ```python class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: - ans = [0] * len(temperatures) stk = [] - for i, t in enumerate(temperatures): - while stk and temperatures[stk[-1]] < t: - j = stk.pop() - ans[j] = i - j + 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 ``` @@ -70,12 +80,14 @@ class Solution: class Solution { public int[] dailyTemperatures(int[] temperatures) { int n = temperatures.length; - int[] ans = new int[n]; Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - while (!stk.isEmpty() && temperatures[stk.peek()] < temperatures[i]) { - int j = stk.pop(); - ans[j] = i - j; + 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); } @@ -91,13 +103,15 @@ class Solution { public: vector dailyTemperatures(vector& temperatures) { int n = temperatures.size(); - vector ans(n); stack stk; - for (int i = 0; i < n; ++i) { - while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) { - ans[stk.top()] = i - stk.top(); + 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; @@ -109,14 +123,16 @@ public: ```go func dailyTemperatures(temperatures []int) []int { - ans := make([]int, len(temperatures)) - var stk []int - for i, t := range temperatures { - for len(stk) > 0 && temperatures[stk[len(stk)-1]] < t { - j := stk[len(stk)-1] - ans[j] = i - j + n := len(temperatures) + ans := make([]int, n) + stk := []int{} + for i := n - 1; i >= 0; i-- { + for len(stk) > 0 && temperatures[stk[len(stk)-1]] <= temperatures[i] { stk = stk[:len(stk)-1] } + if len(stk) > 0 { + ans[i] = stk[len(stk)-1] - i + } stk = append(stk, i) } return ans @@ -128,14 +144,14 @@ func dailyTemperatures(temperatures []int) []int { ```ts function dailyTemperatures(temperatures: number[]): number[] { const n = temperatures.length; - const ans = new Array(n).fill(0); + const ans: number[] = Array(n).fill(0); const stk: number[] = []; - for (let i = n - 1; i >= 0; --i) { - while (stk.length && temperatures[stk[stk.length - 1]] <= temperatures[i]) { + for (let i = n - 1; ~i; --i) { + while (stk.length && temperatures[stk.at(-1)!] <= temperatures[i]) { stk.pop(); } if (stk.length) { - ans[i] = stk[stk.length - 1] - i; + ans[i] = stk.at(-1)! - i; } stk.push(i); } @@ -149,16 +165,24 @@ function dailyTemperatures(temperatures: number[]): number[] { impl Solution { pub fn daily_temperatures(temperatures: Vec) -> Vec { let n = temperatures.len(); - let mut stack = vec![]; - let mut res = vec![0; n]; - for i in 0..n { - while !stack.is_empty() && temperatures[*stack.last().unwrap()] < temperatures[i] { - let j = stack.pop().unwrap(); - res[j] = (i - j) as i32; + let mut stk: Vec = Vec::new(); + let mut ans = vec![0; n]; + + for i in (0..n).rev() { + while let Some(&top) = stk.last() { + if temperatures[top] <= temperatures[i] { + stk.pop(); + } else { + break; + } } - stack.push(i); + if let Some(&top) = stk.last() { + ans[i] = (top - i) as i32; + } + stk.push(i); } - res + + ans } } ``` @@ -172,14 +196,14 @@ impl Solution { */ var dailyTemperatures = function (temperatures) { const n = temperatures.length; - const ans = new Array(n).fill(0); + const ans = Array(n).fill(0); const stk = []; - for (let i = n - 1; i >= 0; --i) { - while (stk.length && temperatures[stk[stk.length - 1]] <= temperatures[i]) { + for (let i = n - 1; ~i; --i) { + while (stk.length && temperatures[stk.at(-1)] <= temperatures[i]) { stk.pop(); } if (stk.length) { - ans[i] = stk[stk.length - 1] - i; + ans[i] = stk.at(-1) - i; } stk.push(i); } @@ -191,92 +215,4 @@ var dailyTemperatures = function (temperatures) { - - -### Solution 2 - - - -#### Python3 - -```python -class Solution: - def dailyTemperatures(self, temperatures: List[int]) -> List[int]: - n = len(temperatures) - stk = [] - 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(); - vector ans(n); - stack stk; - 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; - } -}; -``` - -#### Go - -```go -func dailyTemperatures(temperatures []int) []int { - n := len(temperatures) - ans := make([]int, n) - var stk []int - for i := n - 1; i >= 0; i-- { - for len(stk) > 0 && temperatures[stk[len(stk)-1]] <= temperatures[i] { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - ans[i] = stk[len(stk)-1] - i - } - stk = append(stk, i) - } - return ans -} -``` - - - - - diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution.cpp b/solution/0700-0799/0739.Daily Temperatures/Solution.cpp index 7b71f8274ef28..5f88ac5ed4f67 100644 --- a/solution/0700-0799/0739.Daily Temperatures/Solution.cpp +++ b/solution/0700-0799/0739.Daily Temperatures/Solution.cpp @@ -2,15 +2,17 @@ class Solution { public: vector dailyTemperatures(vector& temperatures) { int n = temperatures.size(); - vector ans(n); stack stk; - for (int i = 0; i < n; ++i) { - while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) { - ans[stk.top()] = i - stk.top(); + 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 +}; diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution.go b/solution/0700-0799/0739.Daily Temperatures/Solution.go index 346f017c83fbe..40094f4b33433 100644 --- a/solution/0700-0799/0739.Daily Temperatures/Solution.go +++ b/solution/0700-0799/0739.Daily Temperatures/Solution.go @@ -1,13 +1,15 @@ func dailyTemperatures(temperatures []int) []int { - ans := make([]int, len(temperatures)) - var stk []int - for i, t := range temperatures { - for len(stk) > 0 && temperatures[stk[len(stk)-1]] < t { - j := stk[len(stk)-1] - ans[j] = i - j + n := len(temperatures) + ans := make([]int, n) + stk := []int{} + for i := n - 1; i >= 0; i-- { + for len(stk) > 0 && temperatures[stk[len(stk)-1]] <= temperatures[i] { stk = stk[:len(stk)-1] } + if len(stk) > 0 { + ans[i] = stk[len(stk)-1] - i + } stk = append(stk, i) } return ans -} \ No newline at end of file +} diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution.java b/solution/0700-0799/0739.Daily Temperatures/Solution.java index 8be99dca42659..34fe102acebb2 100644 --- a/solution/0700-0799/0739.Daily Temperatures/Solution.java +++ b/solution/0700-0799/0739.Daily Temperatures/Solution.java @@ -1,15 +1,17 @@ class Solution { public int[] dailyTemperatures(int[] temperatures) { int n = temperatures.length; - int[] ans = new int[n]; Deque stk = new ArrayDeque<>(); - for (int i = 0; i < n; ++i) { - while (!stk.isEmpty() && temperatures[stk.peek()] < temperatures[i]) { - int j = stk.pop(); - ans[j] = i - j; + 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; } -} \ No newline at end of file +} diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution.js b/solution/0700-0799/0739.Daily Temperatures/Solution.js index 2d18b58d65b34..cf3bf9cec9ba1 100644 --- a/solution/0700-0799/0739.Daily Temperatures/Solution.js +++ b/solution/0700-0799/0739.Daily Temperatures/Solution.js @@ -4,14 +4,14 @@ */ var dailyTemperatures = function (temperatures) { const n = temperatures.length; - const ans = new Array(n).fill(0); + const ans = Array(n).fill(0); const stk = []; - for (let i = n - 1; i >= 0; --i) { - while (stk.length && temperatures[stk[stk.length - 1]] <= temperatures[i]) { + for (let i = n - 1; ~i; --i) { + while (stk.length && temperatures[stk.at(-1)] <= temperatures[i]) { stk.pop(); } if (stk.length) { - ans[i] = stk[stk.length - 1] - i; + ans[i] = stk.at(-1) - i; } stk.push(i); } diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution.py b/solution/0700-0799/0739.Daily Temperatures/Solution.py index d477937de4dd3..89b002280d28b 100644 --- a/solution/0700-0799/0739.Daily Temperatures/Solution.py +++ b/solution/0700-0799/0739.Daily Temperatures/Solution.py @@ -1,10 +1,12 @@ class Solution: def dailyTemperatures(self, temperatures: List[int]) -> List[int]: - ans = [0] * len(temperatures) stk = [] - for i, t in enumerate(temperatures): - while stk and temperatures[stk[-1]] < t: - j = stk.pop() - ans[j] = i - j + 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 diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution.rs b/solution/0700-0799/0739.Daily Temperatures/Solution.rs index ad6c1b5a83c2a..89a18992ddc40 100644 --- a/solution/0700-0799/0739.Daily Temperatures/Solution.rs +++ b/solution/0700-0799/0739.Daily Temperatures/Solution.rs @@ -1,15 +1,23 @@ impl Solution { pub fn daily_temperatures(temperatures: Vec) -> Vec { let n = temperatures.len(); - let mut stack = vec![]; - let mut res = vec![0; n]; - for i in 0..n { - while !stack.is_empty() && temperatures[*stack.last().unwrap()] < temperatures[i] { - let j = stack.pop().unwrap(); - res[j] = (i - j) as i32; + let mut stk: Vec = Vec::new(); + let mut ans = vec![0; n]; + + for i in (0..n).rev() { + while let Some(&top) = stk.last() { + if temperatures[top] <= temperatures[i] { + stk.pop(); + } else { + break; + } } - stack.push(i); + if let Some(&top) = stk.last() { + ans[i] = (top - i) as i32; + } + stk.push(i); } - res + + ans } } diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution.ts b/solution/0700-0799/0739.Daily Temperatures/Solution.ts index 2de56fecf69e6..140cbead2a4c0 100644 --- a/solution/0700-0799/0739.Daily Temperatures/Solution.ts +++ b/solution/0700-0799/0739.Daily Temperatures/Solution.ts @@ -1,13 +1,13 @@ function dailyTemperatures(temperatures: number[]): number[] { const n = temperatures.length; - const ans = new Array(n).fill(0); + const ans: number[] = Array(n).fill(0); const stk: number[] = []; - for (let i = n - 1; i >= 0; --i) { - while (stk.length && temperatures[stk[stk.length - 1]] <= temperatures[i]) { + for (let i = n - 1; ~i; --i) { + while (stk.length && temperatures[stk.at(-1)!] <= temperatures[i]) { stk.pop(); } if (stk.length) { - ans[i] = stk[stk.length - 1] - i; + ans[i] = stk.at(-1)! - i; } stk.push(i); } diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution2.cpp b/solution/0700-0799/0739.Daily Temperatures/Solution2.cpp deleted file mode 100644 index 3ad97088685ab..0000000000000 --- a/solution/0700-0799/0739.Daily Temperatures/Solution2.cpp +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - vector dailyTemperatures(vector& temperatures) { - int n = temperatures.size(); - vector ans(n); - stack stk; - 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 diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution2.go b/solution/0700-0799/0739.Daily Temperatures/Solution2.go deleted file mode 100644 index ff6cf7e4efb4f..0000000000000 --- a/solution/0700-0799/0739.Daily Temperatures/Solution2.go +++ /dev/null @@ -1,15 +0,0 @@ -func dailyTemperatures(temperatures []int) []int { - n := len(temperatures) - ans := make([]int, n) - var stk []int - for i := n - 1; i >= 0; i-- { - for len(stk) > 0 && temperatures[stk[len(stk)-1]] <= temperatures[i] { - stk = stk[:len(stk)-1] - } - if len(stk) > 0 { - ans[i] = stk[len(stk)-1] - i - } - stk = append(stk, i) - } - return ans -} \ No newline at end of file diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution2.java b/solution/0700-0799/0739.Daily Temperatures/Solution2.java deleted file mode 100644 index 7eda02cd99b08..0000000000000 --- a/solution/0700-0799/0739.Daily Temperatures/Solution2.java +++ /dev/null @@ -1,17 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution2.py b/solution/0700-0799/0739.Daily Temperatures/Solution2.py deleted file mode 100644 index e5a9ba4f87332..0000000000000 --- a/solution/0700-0799/0739.Daily Temperatures/Solution2.py +++ /dev/null @@ -1,12 +0,0 @@ -class Solution: - def dailyTemperatures(self, temperatures: List[int]) -> List[int]: - n = len(temperatures) - stk = [] - 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