-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (27 loc) · 854 Bytes
/
Solution.java
File metadata and controls
35 lines (27 loc) · 854 Bytes
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
import java.util.Stack;
public class Solution {
public static void main(String[] args){
int[] heights = {6,9,5,7,4};
int[] answer = solution(heights);
for(int num : answer){
System.out.print(num + " ");
}
}
public static int[] solution(int[] heights){
int[] answer = new int[heights.length];
Stack<Integer> tower = new Stack<Integer>();
for(int i=0; i<heights.length; i++){
tower.push(heights[i]);
}
while(!tower.isEmpty()){
int sender = tower.pop();
for(int i=tower.size(); i>=0; i--){
if(sender < heights[i]){
answer[tower.size()] = i+1;
break;
}
}
}
return answer;
}
}