File tree 5 files changed +60
-0
lines changed
algorithms/DailyTemperatures
5 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -301,6 +301,7 @@ All solutions will be accepted!
301
301
| 134| [ Gas Station] ( https://leetcode-cn.com/problems/gas-station/description/ ) | [ java/py/js] ( ./algorithms/GasStation ) | Medium|
302
302
| 238| [ Product Of Array Except Self] ( https://leetcode-cn.com/problems/product-of-array-except-self/description/ ) | [ java/py/js] ( ./algorithms/ProductOfArrayExceptSelf ) | Medium|
303
303
| 896| [ Monotonic Array] ( https://leetcode-cn.com/problems/monotonic-array/description/ ) | [ java/py/js] ( ./algorithms/MonotonicArray ) | Easy|
304
+ | 739| [ Daily Temperatures] ( https://leetcode-cn.com/problems/daily-temperatures/description/ ) | [ java/py/js] ( ./algorithms/DailyTemperatures ) | Medium|
304
305
305
306
# Database
306
307
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Daily Temperatures
2
+ We can solve this problem by stack and hashmap
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int [] dailyTemperatures (int [] temperatures ) {
3
+ int length = temperatures .length ;
4
+ int [] res = new int [length ];
5
+ LinkedList <Map <String , Integer >> stack = new LinkedList <Map <String , Integer >>();
6
+
7
+ for (int i = 0 ; i < length ; i ++) {
8
+ while (stack .size () > 0 && stack .peek ().get ("value" ) < temperatures [i ]) {
9
+ Map <String , Integer > e = stack .pop ();
10
+ res [e .get ("index" )] = i - e .get ("index" );
11
+ }
12
+ Map <String , Integer > e = new HashMap <String , Integer >();
13
+ e .put ("index" , i );
14
+ e .put ("value" , temperatures [i ]);
15
+ stack .push (e );
16
+ }
17
+
18
+ return res ;
19
+ }
20
+ }
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
+ let length = temperatures . length ,
7
+ res = new Array ( length ) ,
8
+ stack = [ ]
9
+ res . fill ( 0 )
10
+
11
+ for ( let i = 0 ; i < length ; i ++ ) {
12
+ while ( stack . length > 0 && stack [ stack . length - 1 ] . value < temperatures [ i ] ) {
13
+ let e = stack . pop ( )
14
+ res [ e . index ] = i - e . index
15
+ }
16
+ stack . push ( { index : i , value : temperatures [ i ] } )
17
+ }
18
+
19
+ return res
20
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def dailyTemperatures (self , temperatures ):
3
+ """
4
+ :type temperatures: List[int]
5
+ :rtype: List[int]
6
+ """
7
+ length = len (temperatures )
8
+ res = [0 ] * length
9
+ stack = []
10
+
11
+ for i in xrange (length ):
12
+ while len (stack ) > 0 and stack [- 1 ]['value' ] < temperatures [i ]:
13
+ e = stack .pop ()
14
+ res [e ['index' ]] = i - e ['index' ]
15
+ stack .append ({ 'index' : i , 'value' : temperatures [i ] })
16
+
17
+ return res
You can’t perform that action at this time.
0 commit comments