forked from Nikhil-2002/Programming_Hactoberfest23
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNGE.java
22 lines (22 loc) · 716 Bytes
/
NGE.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//leetcode --> 496. Next Greater Element I
import java.util.*;
class Hactober {
public static int[] nextGreaterElement(int[] nums1, int[] nums2) {
HashMap<Integer,Integer> map = new HashMap<>();
Stack<Integer> s = new Stack<>();
for(int i:nums2){
while(!s.isEmpty()&&s.peek()<i)map.put(s.pop(),i);
s.push(i);
}
for(int i=0;i<nums1.length;i++){
nums1[i]=map.getOrDefault(nums1[i],-1);
}
return nums1;
}
public static void main(String[] args){
int[] arr1 = {4,1,2};
int[] arr2 = {1,3,4,2};
int[] res = nextGreaterElement(arr1,arr2);
for(int i : res)System.out.print(i + " ");
}
}