From a36e3c9a065f484960fca0293b5f4b42e0311d4a Mon Sep 17 00:00:00 2001
From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com>
Date: Tue, 15 Apr 2025 07:15:07 +0530
Subject: [PATCH 1/6] Create 739. Daily Temperatures.py
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com>
Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
---
Solution/739. Daily Temperatures/739. Daily Temperatures.py | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Solution/739. Daily Temperatures/739. Daily Temperatures.py
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..e69de29
From 63a96bca82bc366c7a81c9e1693dd9e2cdf22ed3 Mon Sep 17 00:00:00 2001
From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com>
Date: Tue, 15 Apr 2025 07:15:19 +0530
Subject: [PATCH 2/6] Update 739. Daily Temperatures.py
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com>
Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
---
.../739. Daily Temperatures.py | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/Solution/739. Daily Temperatures/739. Daily Temperatures.py b/Solution/739. Daily Temperatures/739. Daily Temperatures.py
index e69de29..b0ee276 100644
--- a/Solution/739. Daily Temperatures/739. Daily Temperatures.py
+++ 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
From 614bd1db6427791dc56297f7748ed66396964a29 Mon Sep 17 00:00:00 2001
From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com>
Date: Tue, 15 Apr 2025 07:15:48 +0530
Subject: [PATCH 3/6] Create readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com>
Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
---
Solution/739. Daily Temperatures/readme.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Solution/739. Daily Temperatures/readme.md
diff --git a/Solution/739. Daily Temperatures/readme.md b/Solution/739. Daily Temperatures/readme.md
new file mode 100644
index 0000000..e69de29
From ffcc9f6b5a7497731bfe084d0727330e6fbab9f1 Mon Sep 17 00:00:00 2001
From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com>
Date: Tue, 15 Apr 2025 07:15:55 +0530
Subject: [PATCH 4/6] Update readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com>
Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
---
Solution/739. Daily Temperatures/readme.md | 218 +++++++++++++++++++++
1 file changed, 218 insertions(+)
diff --git a/Solution/739. Daily Temperatures/readme.md b/Solution/739. Daily Temperatures/readme.md
index e69de29..70b0def 100644
--- a/Solution/739. Daily Temperatures/readme.md
+++ b/Solution/739. Daily Temperatures/readme.md
@@ -0,0 +1,218 @@
+---
+comments: true
+difficulty: Medium
+edit_url: https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0739.Daily%20Temperatures/README_EN.md
+tags:
+ - Stack
+ - Array
+ - Monotonic Stack
+---
+
+
+
+# [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures)
+
+[中文文档](/solution/0700-0799/0739.Daily%20Temperatures/README.md)
+
+## 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:
+
+
+ 1 <= temperatures.length <= 105
+ 30 <= temperatures[i] <= 100
+
+
+
+
+## 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;
+ }
+};
+```
+
+#### Go
+
+```go
+func dailyTemperatures(temperatures []int) []int {
+ 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
+}
+```
+
+#### TypeScript
+
+```ts
+function dailyTemperatures(temperatures: number[]): number[] {
+ const n = temperatures.length;
+ const ans: number[] = Array(n).fill(0);
+ const stk: number[] = [];
+ for (let i = n - 1; ~i; --i) {
+ while (stk.length && temperatures[stk.at(-1)!] <= temperatures[i]) {
+ stk.pop();
+ }
+ if (stk.length) {
+ ans[i] = stk.at(-1)! - i;
+ }
+ stk.push(i);
+ }
+ return ans;
+}
+```
+
+#### 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
+/**
+ * @param {number[]} temperatures
+ * @return {number[]}
+ */
+var dailyTemperatures = function (temperatures) {
+ const n = temperatures.length;
+ const ans = Array(n).fill(0);
+ const stk = [];
+ for (let i = n - 1; ~i; --i) {
+ while (stk.length && temperatures[stk.at(-1)] <= temperatures[i]) {
+ stk.pop();
+ }
+ if (stk.length) {
+ ans[i] = stk.at(-1) - i;
+ }
+ stk.push(i);
+ }
+ return ans;
+};
+```
+
+
+
+
+
+
\ No newline at end of file
From 10d6e395c4255359fdc0c110f06e0a4f5df2a011 Mon Sep 17 00:00:00 2001
From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com>
Date: Tue, 15 Apr 2025 07:16:15 +0530
Subject: [PATCH 5/6] Update readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com>
Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
---
Solution/739. Daily Temperatures/readme.md | 92 ----------------------
1 file changed, 92 deletions(-)
diff --git a/Solution/739. Daily Temperatures/readme.md b/Solution/739. Daily Temperatures/readme.md
index 70b0def..065de7a 100644
--- a/Solution/739. Daily Temperatures/readme.md
+++ b/Solution/739. Daily Temperatures/readme.md
@@ -119,98 +119,6 @@ public:
};
```
-#### Go
-
-```go
-func dailyTemperatures(temperatures []int) []int {
- 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
-}
-```
-
-#### TypeScript
-
-```ts
-function dailyTemperatures(temperatures: number[]): number[] {
- const n = temperatures.length;
- const ans: number[] = Array(n).fill(0);
- const stk: number[] = [];
- for (let i = n - 1; ~i; --i) {
- while (stk.length && temperatures[stk.at(-1)!] <= temperatures[i]) {
- stk.pop();
- }
- if (stk.length) {
- ans[i] = stk.at(-1)! - i;
- }
- stk.push(i);
- }
- return ans;
-}
-```
-
-#### 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
-/**
- * @param {number[]} temperatures
- * @return {number[]}
- */
-var dailyTemperatures = function (temperatures) {
- const n = temperatures.length;
- const ans = Array(n).fill(0);
- const stk = [];
- for (let i = n - 1; ~i; --i) {
- while (stk.length && temperatures[stk.at(-1)] <= temperatures[i]) {
- stk.pop();
- }
- if (stk.length) {
- ans[i] = stk.at(-1) - i;
- }
- stk.push(i);
- }
- return ans;
-};
-```
-
From a0cb7e5aceedc40057bf7af178d1ea8fe9b90b20 Mon Sep 17 00:00:00 2001
From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com>
Date: Tue, 15 Apr 2025 07:16:33 +0530
Subject: [PATCH 6/6] Update readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com>
Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
---
Solution/739. Daily Temperatures/readme.md | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/Solution/739. Daily Temperatures/readme.md b/Solution/739. Daily Temperatures/readme.md
index 065de7a..9da65d9 100644
--- a/Solution/739. Daily Temperatures/readme.md
+++ b/Solution/739. Daily Temperatures/readme.md
@@ -1,7 +1,7 @@
---
comments: true
difficulty: Medium
-edit_url: https://github.com/doocs/leetcode/edit/main/solution/0700-0799/0739.Daily%20Temperatures/README_EN.md
+edit_url: Antim
tags:
- Stack
- Array
@@ -12,8 +12,6 @@ tags:
# [739. Daily Temperatures](https://leetcode.com/problems/daily-temperatures)
-[中文文档](/solution/0700-0799/0739.Daily%20Temperatures/README.md)
-
## Description