We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d8a6969 commit 2bbca41Copy full SHA for 2bbca41
NGE.java
@@ -0,0 +1,22 @@
1
+//leetcode --> 496. Next Greater Element I
2
+import java.util.*;
3
+class Hactober {
4
+ public static int[] nextGreaterElement(int[] nums1, int[] nums2) {
5
+ HashMap<Integer,Integer> map = new HashMap<>();
6
+ Stack<Integer> s = new Stack<>();
7
+ for(int i:nums2){
8
+ while(!s.isEmpty()&&s.peek()<i)map.put(s.pop(),i);
9
+ s.push(i);
10
+ }
11
+ for(int i=0;i<nums1.length;i++){
12
+ nums1[i]=map.getOrDefault(nums1[i],-1);
13
14
+ return nums1;
15
16
+ public static void main(String[] args){
17
+ int[] arr1 = {4,1,2};
18
+ int[] arr2 = {1,3,4,2};
19
+ int[] res = nextGreaterElement(arr1,arr2);
20
+ for(int i : res)System.out.print(i + " ");
21
22
+}
0 commit comments