-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextGreater1.java
More file actions
57 lines (48 loc) · 1.47 KB
/
NextGreater1.java
File metadata and controls
57 lines (48 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package HashTable;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
/**
* Author - archit.s
* Date - 27/08/18
* Time - 7:16 PM
*/
public class NextGreater1 {
// Solution using Stacks (O(n))
//
// public int[] nextGreaterElement(int[] findNums, int[] nums) {
// Map<Integer, Integer> map = new HashMap<>(); // map from x to next greater element of x
// Stack<Integer> stack = new Stack<>();
// for (int num : nums) {
// while (!stack.isEmpty() && stack.peek() < num)
// map.put(stack.pop(), num);
// stack.push(num);
// }
// for (int i = 0; i < findNums.length; i++)
// findNums[i] = map.getOrDefault(findNums[i], -1);
// return findNums;
// }
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Map<Integer, Integer> map = new HashMap<>();
if(nums2.length == 0){
return new int[0];
}
map.put(nums2[nums2.length-1], -1);
for(int i=nums2.length-2;i>=0;i--){
if(nums2[i+1] > nums2[i]){
map.put(nums2[i], nums2[i+1]);
}
else{
int n = map.get(nums2[i+1]);
while(n < nums2[i] && n != -1){
n = map.get(n);
}
map.put(nums2[i], n);
}
}
for(int i=0;i<nums1.length;i++){
nums1[i] = map.get(nums1[i]);
}
return nums1;
}
}