File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -24,3 +24,4 @@ https://neetcode.io/roadmap
24
24
| 155 | [ Min Stack] ( https://leetcode.com/problems/min-stack/ ) | Medium | [ TypeScript] ( ./TypeScript/155.min-stack.ts ) | Stack |
25
25
| 150 | [ Evaluate Reverse Polish Notation] ( https://leetcode.com/problems/evaluate-reverse-polish-notation/ ) | Medium | [ TypeScript] ( ./TypeScript/150.evaluate-reverse-polish-notation.ts ) | Stack |
26
26
| 22 | [ Generate Parentheses] ( https://leetcode.com/problems/generate-parentheses/ ) | Medium | [ TypeScript] ( ./TypeScript/22.generate-parentheses.ts ) | Stack |
27
+ | 739 | [ Daily Temperatures] ( https://leetcode.com/problems/daily-temperatures/ ) | Medium | [ TypeScript] ( ./TypeScript/739.daily-temperatures.ts ) | Stack |
Original file line number Diff line number Diff line change
1
+ function dailyTemperatures ( temperatures : number [ ] ) : number [ ] {
2
+ const result : number [ ] = [ ] ;
3
+
4
+ for ( let i = 0 ; i < temperatures . length ; i ++ ) {
5
+ for ( let j = i ; j < temperatures . length ; j ++ ) {
6
+ if ( temperatures [ j ] > temperatures [ i ] ) {
7
+ result [ i ] = j - i ;
8
+ break ;
9
+ }
10
+ if ( j === temperatures . length - 1 ) {
11
+ result [ i ] = 0 ;
12
+ }
13
+ }
14
+ }
15
+
16
+ return result ;
17
+ }
18
+
19
+ type TemperatureValue = number ;
20
+ type TemperatureIndex = number ;
21
+ type TemperatureItem = [ TemperatureValue , TemperatureIndex ] ;
22
+
23
+ function dailyTemperatures2 ( temperatures : number [ ] ) : number [ ] {
24
+ const result : number [ ] = new Array ( temperatures . length ) . fill ( 0 ) ;
25
+ const stack : TemperatureItem [ ] = [ ] ;
26
+
27
+ for ( let i = 0 ; i < temperatures . length ; i ++ ) {
28
+ while ( stack . length && temperatures [ i ] > stack [ stack . length - 1 ] [ 0 ] ) {
29
+ const [ _ , index ] = stack . pop ( ) ! ;
30
+ result [ index ] = i - index ;
31
+ }
32
+ stack . push ( [ temperatures [ i ] , i ] ) ;
33
+ }
34
+
35
+ return result ;
36
+ }
You can’t perform that action at this time.
0 commit comments