forked from AkashMarkad/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNext Greater Element.java
More file actions
53 lines (38 loc) · 1.22 KB
/
Next Greater Element.java
File metadata and controls
53 lines (38 loc) · 1.22 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
// Next Greater Element
// Given array we need to find the frist greter elemnt on right
// Approach 1: Brute Force
// Use For loop and check entire array for the first greater element on right
// Time Complexity- O(n*n)
// Space Complexity - O(1)
// Approach 2: Use Stack
// Time Complexity - O(2n)
// Space Compelxity- o(n)
// Code:
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int[] ans = new int[nums1.length];
int n = nums2.length;
Stack<Integer> st = new Stack<>();
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i=n-1;i>=0;i--){
while(!st.isEmpty() && nums2[i] >= st.peek()){
st.pop();
}
if(i<n){
if(!st.isEmpty()){
hm.put(nums2[i], st.peek());
}
else{
hm.put(nums2[i] , -1);
}
}
st.push(nums2[i]);
}
for(int i=0;i<nums1.length;i++){
if(hm.containsKey(nums1[i])){
ans[i] = hm.get(nums1[i]);
}
}
return ans;
}
}