-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextGreater.java
More file actions
54 lines (49 loc) · 1.36 KB
/
Copy pathnextGreater.java
File metadata and controls
54 lines (49 loc) · 1.36 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
/**
* Given an array, find the next greater element G[i] for
* every element A[i] in the array.
* The Next greater Element for an element A[i] is the first
* greater element on the right side of A[i] in array.
*/
import java.util.*;
// Doing this as I need to have the index of the pushed item from the given array
class item {
int index;
Integer value;
item(int i, Integer v){index = i; value = v;}
}
public class Solution {
public ArrayList<Integer> nextGreater(ArrayList<Integer> a) {
int i,s;
Integer element,next;
item I,It;
Stack st = new Stack();
s = a.size();
ArrayList<Integer> result = a;
I = new item(0,new Integer(a.get(0)));
st.push(I);
for(i=1; i < s; i++){
next = a.get(i);
if(!st.empty()){
It = (item) st.pop();
element = It.value;
while(element < next){
result.set(It.index,next);
if(st.empty()) break;
It = (item) st.pop();
element = It.value;
}
if(element >= next){
st.push(It);
}
}
I = new item(i,next);
st.push(I);
}
while(!st.empty()){
It = (item) st.pop();
element = It.value;
result.set(It.index, new Integer(-1));
}
return result;
}
}