Skip to content

Commit

Permalink
Q496 added.
Browse files Browse the repository at this point in the history
  • Loading branch information
isinsuarici committed Mar 11, 2023
1 parent 0649fd7 commit 1f984a5
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions E_496_NextGreaterElementI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer,Integer> map= new HashMap<>();
Stack<Integer> stack = new Stack<>();

for(int num: nums2){
while(!stack.isEmpty() && stack.peek()<num){
map.put(stack.pop(),num);
}
stack.push(num);
}
for(int i=0;i<nums1.length;i++)
nums1[i]=map.getOrDefault(nums1[i],-1);
return nums1;
}
}

0 comments on commit 1f984a5

Please sign in to comment.