File tree Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int [] nextGreaterElement (int [] nums1 , int [] nums2 ) {
3
3
Stack <Integer > stack = new Stack <>();
4
- int [] greater = new int [nums2 .length ];
5
- Map <Integer , Integer > map = new HashMap <>();
4
+ int [] nextMaximum = new int [nums2 .length ];
5
+ Map <Integer , Integer > indexMap = new HashMap <>();
6
6
for (int i = nums2 .length - 1 ; i >= 0 ; i --) {
7
- while (!stack .isEmpty () && stack .peek () < nums2 [i ]) {
7
+ while (!stack .isEmpty () && stack .peek () <= nums2 [i ]) {
8
8
stack .pop ();
9
9
}
10
- greater [i ] = stack .isEmpty () ? -1 : stack .peek ();
11
- map .put (nums2 [i ], i );
10
+ nextMaximum [i ] = stack .isEmpty () ? -1 : stack .peek ();
12
11
stack .push (nums2 [i ]);
12
+ indexMap .put (nums2 [i ], i );
13
13
}
14
- int [] ans = new int [nums1 .length ];
14
+ int [] nextMaximumResult = new int [nums1 .length ];
15
15
for (int i = 0 ; i < nums1 .length ; i ++) {
16
- ans [i ] = greater [ map .get (nums1 [i ])];
16
+ nextMaximumResult [i ] = nextMaximum [ indexMap .get (nums1 [i ])];
17
17
}
18
- return ans ;
18
+ return nextMaximumResult ;
19
19
}
20
20
}
You can’t perform that action at this time.
0 commit comments