import java.util.Stack;
public class Top {
public int[] solution(int[] heights) {
Stack<Integer> stack = new Stack<Integer>();
boolean flag;
for (int i = heights.length - 1; i >= 1; i--) {
flag = false;
for (int j = i - 1; j >= 0; j--) {
if (heights[i] < heights[j]) {
stack.push(j + 1);
flag = true;
break;
}
}
if (!flag) {
stack.push(0);
}
}
stack.push(0);
int[] answer = new int[heights.length];
for (int i = 0; i < answer.length; i++) {
answer[i] = stack.pop();
}
return answer;
}
public static void main(String[] args) {
Top s = new Top();
int[] arr1 = { 6, 9, 5, 7, 4 };
int[] arr2 = { 3, 9, 9, 3, 5, 7, 2 };
int[] arr3 = { 1, 5, 3, 6, 7, 6, 5 };
s.solution(arr1);
s.solution(arr2);
s.solution(arr3);
}
}