File tree Expand file tree Collapse file tree 1 file changed +12
-11
lines changed Expand file tree Collapse file tree 1 file changed +12
-11
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
- public int [] dailyTemperatures (int [] T ) {
3
- Stack <Integer > stack = new Stack <>();
4
- int [] ans = new int [T .length ];
5
- for (int i = T .length - 1 ; i >= 0 ; i --) {
6
- while (!stack .isEmpty () && T [stack .peek ()] <= T [i ]) {
7
- stack .pop ();
8
- }
9
- ans [i ] = stack .isEmpty () ? 0 : (stack .peek () - i );
10
- stack .push (i );
2
+ public int [] dailyTemperatures (int [] temperatures ) {
3
+ int n = temperatures .length ;
4
+ int [] result = new int [n ];
5
+ Stack <Integer > stack = new Stack <>();
6
+ for (int i = n - 1 ; i >= 0 ; i --) {
7
+ while (!stack .isEmpty () && temperatures [i ] >= temperatures [stack .peek ()]) {
8
+ stack .pop ();
9
+ }
10
+ result [i ] = stack .isEmpty () ? 0 : stack .peek () - i ;
11
+ stack .push (i );
12
+ }
13
+ return result ;
11
14
}
12
- return ans ;
13
- }
14
15
}
You can’t perform that action at this time.
0 commit comments