File tree Expand file tree Collapse file tree 4 files changed +146
-0
lines changed
solution/0700-0799/0739.Daily Temperatures Expand file tree Collapse file tree 4 files changed +146
-0
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,12 @@ for i in range(n):
55
55
stk.append(i)
56
56
```
57
57
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
+
58
64
<!-- tabs:start -->
59
65
60
66
### ** Python3**
@@ -208,6 +214,33 @@ func dailyTemperatures(temperatures []int) []int {
208
214
}
209
215
```
210
216
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
+
211
244
### ** Rust**
212
245
213
246
``` rust
@@ -228,6 +261,29 @@ impl Solution {
228
261
}
229
262
```
230
263
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
+
231
287
### ** ...**
232
288
233
289
```
Original file line number Diff line number Diff line change @@ -182,6 +182,33 @@ func dailyTemperatures(temperatures []int) []int {
182
182
}
183
183
```
184
184
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
+
185
212
### ** Rust**
186
213
187
214
``` rust
@@ -202,6 +229,29 @@ impl Solution {
202
229
}
203
230
```
204
231
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
+
205
255
### ** ...**
206
256
207
257
```
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments