-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.java
43 lines (30 loc) · 937 Bytes
/
main.java
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
package stack2;
import java.util.Arrays;
import java.util.Stack;
public class main {
public static void main(String[] args) {
Solution solution = new Solution();
int[] ret = solution.solution(new int[]{1,5,3,6,7,6,5});
System.out.println(Arrays.toString(ret));
}
}
class Solution {
public int[] solution(int[] heights) {
int[] answer = new int[heights.length];
Stack<int[]> stack = new Stack<>();
for(int i=0; i<heights.length; i++){
int receivedIndex = 0;
while(!stack.isEmpty()){
int[] compare = stack.peek();
if(compare[1] > heights[i]){
receivedIndex = compare[0];
break;
}
stack.pop();
}
stack.push(new int[]{i+1,heights[i]});
answer[i] = receivedIndex;
}
return answer;
}
}