File tree 1 file changed +5
-7
lines changed
1 file changed +5
-7
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
+ Map <Integer , Integer > map = new HashMap <>();
3
4
Stack <Integer > stack = new Stack <>();
4
- int [] nextMaximum = new int [nums2 .length ];
5
- Map <Integer , Integer > indexMap = new HashMap <>();
6
5
for (int i = nums2 .length - 1 ; i >= 0 ; i --) {
7
6
while (!stack .isEmpty () && stack .peek () <= nums2 [i ]) {
8
7
stack .pop ();
9
8
}
10
- nextMaximum [i ] = stack .isEmpty () ? -1 : stack .peek ();
9
+ map . put ( nums2 [i ], stack .isEmpty () ? -1 : stack .peek () );
11
10
stack .push (nums2 [i ]);
12
- indexMap .put (nums2 [i ], i );
13
11
}
14
- int [] nextMaximumResult = new int [nums1 .length ];
12
+ int [] result = new int [nums1 .length ];
15
13
for (int i = 0 ; i < nums1 .length ; i ++) {
16
- nextMaximumResult [i ] = nextMaximum [ indexMap . get (nums1 [i ])] ;
14
+ result [i ] = map . getOrDefault (nums1 [i ], - 1 ) ;
17
15
}
18
- return nextMaximumResult ;
16
+ return result ;
19
17
}
20
18
}
You can’t perform that action at this time.
0 commit comments