Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 20ab188

Browse files
authored
Update Next Greater Element I.java
1 parent e2dae45 commit 20ab188

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Easy/Next Greater Element I.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Solution {
22
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
33
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<>();
66
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]) {
88
stack.pop();
99
}
10-
greater[i] = stack.isEmpty() ? -1 : stack.peek();
11-
map.put(nums2[i], i);
10+
nextMaximum[i] = stack.isEmpty() ? -1 : stack.peek();
1211
stack.push(nums2[i]);
12+
indexMap.put(nums2[i], i);
1313
}
14-
int[] ans = new int[nums1.length];
14+
int[] nextMaximumResult = new int[nums1.length];
1515
for (int i = 0; i < nums1.length; i++) {
16-
ans[i] = greater[map.get(nums1[i])];
16+
nextMaximumResult[i] = nextMaximum[indexMap.get(nums1[i])];
1717
}
18-
return ans;
18+
return nextMaximumResult;
1919
}
2020
}

0 commit comments

Comments
 (0)