Skip to content

Commit 0955e26

Browse files
committed
feat: add solutions to lc problem: No.0739
No.0739.Daily Temperatures
1 parent cbca4b1 commit 0955e26

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

solution/0700-0799/0739.Daily Temperatures/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ for i in range(n):
5555
stk.append(i)
5656
```
5757

58+
对于本题,我们需要找出每个数右边**离它最近的****比它大的数**,因此我们可以从右往左遍历数组,且需要维护一个从栈底到栈顶单调递减的栈。
59+
60+
对于当前遍历到的数 `temperatures[i]`,如果栈顶元素 `temperatures[stk[-1]]` 小于等于 `temperatures[i]`,则弹出栈顶元素,直到栈为空或者栈顶元素大于 `temperatures[i]`。如果此时栈不为空,那么栈顶元素就是 `temperatures[i]` 右边离它最近的且比它大的数,更新 `ans[i] = stk[-1] - i`。接着,我们将 $i$ 入栈,继续遍历下一个数。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `temperatures` 数组的长度。
63+
5864
<!-- tabs:start -->
5965

6066
### **Python3**
@@ -208,6 +214,33 @@ func dailyTemperatures(temperatures []int) []int {
208214
}
209215
```
210216

217+
### **JavaScript**
218+
219+
```js
220+
/**
221+
* @param {number[]} temperatures
222+
* @return {number[]}
223+
*/
224+
var dailyTemperatures = function (temperatures) {
225+
const n = temperatures.length;
226+
const ans = new Array(n).fill(0);
227+
const stk = [];
228+
for (let i = n - 1; i >= 0; --i) {
229+
while (
230+
stk.length &&
231+
temperatures[stk[stk.length - 1]] <= temperatures[i]
232+
) {
233+
stk.pop();
234+
}
235+
if (stk.length) {
236+
ans[i] = stk[stk.length - 1] - i;
237+
}
238+
stk.push(i);
239+
}
240+
return ans;
241+
};
242+
```
243+
211244
### **Rust**
212245

213246
```rust
@@ -228,6 +261,29 @@ impl Solution {
228261
}
229262
```
230263

264+
### **TypeScript**
265+
266+
```ts
267+
function dailyTemperatures(temperatures: number[]): number[] {
268+
const n = temperatures.length;
269+
const ans = new Array(n).fill(0);
270+
const stk: number[] = [];
271+
for (let i = n - 1; i >= 0; --i) {
272+
while (
273+
stk.length &&
274+
temperatures[stk[stk.length - 1]] <= temperatures[i]
275+
) {
276+
stk.pop();
277+
}
278+
if (stk.length) {
279+
ans[i] = stk[stk.length - 1] - i;
280+
}
281+
stk.push(i);
282+
}
283+
return ans;
284+
}
285+
```
286+
231287
### **...**
232288

233289
```

solution/0700-0799/0739.Daily Temperatures/README_EN.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,33 @@ func dailyTemperatures(temperatures []int) []int {
182182
}
183183
```
184184

185+
### **JavaScript**
186+
187+
```js
188+
/**
189+
* @param {number[]} temperatures
190+
* @return {number[]}
191+
*/
192+
var dailyTemperatures = function (temperatures) {
193+
const n = temperatures.length;
194+
const ans = new Array(n).fill(0);
195+
const stk = [];
196+
for (let i = n - 1; i >= 0; --i) {
197+
while (
198+
stk.length &&
199+
temperatures[stk[stk.length - 1]] <= temperatures[i]
200+
) {
201+
stk.pop();
202+
}
203+
if (stk.length) {
204+
ans[i] = stk[stk.length - 1] - i;
205+
}
206+
stk.push(i);
207+
}
208+
return ans;
209+
};
210+
```
211+
185212
### **Rust**
186213

187214
```rust
@@ -202,6 +229,29 @@ impl Solution {
202229
}
203230
```
204231

232+
### **TypeScript**
233+
234+
```ts
235+
function dailyTemperatures(temperatures: number[]): number[] {
236+
const n = temperatures.length;
237+
const ans = new Array(n).fill(0);
238+
const stk: number[] = [];
239+
for (let i = n - 1; i >= 0; --i) {
240+
while (
241+
stk.length &&
242+
temperatures[stk[stk.length - 1]] <= temperatures[i]
243+
) {
244+
stk.pop();
245+
}
246+
if (stk.length) {
247+
ans[i] = stk[stk.length - 1] - i;
248+
}
249+
stk.push(i);
250+
}
251+
return ans;
252+
}
253+
```
254+
205255
### **...**
206256

207257
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} temperatures
3+
* @return {number[]}
4+
*/
5+
var dailyTemperatures = function (temperatures) {
6+
const n = temperatures.length;
7+
const ans = new Array(n).fill(0);
8+
const stk = [];
9+
for (let i = n - 1; i >= 0; --i) {
10+
while (
11+
stk.length &&
12+
temperatures[stk[stk.length - 1]] <= temperatures[i]
13+
) {
14+
stk.pop();
15+
}
16+
if (stk.length) {
17+
ans[i] = stk[stk.length - 1] - i;
18+
}
19+
stk.push(i);
20+
}
21+
return ans;
22+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function dailyTemperatures(temperatures: number[]): number[] {
2+
const n = temperatures.length;
3+
const ans = new Array(n).fill(0);
4+
const stk: number[] = [];
5+
for (let i = n - 1; i >= 0; --i) {
6+
while (
7+
stk.length &&
8+
temperatures[stk[stk.length - 1]] <= temperatures[i]
9+
) {
10+
stk.pop();
11+
}
12+
if (stk.length) {
13+
ans[i] = stk[stk.length - 1] - i;
14+
}
15+
stk.push(i);
16+
}
17+
return ans;
18+
}

0 commit comments

Comments
 (0)