스택/큐를 이용하지 않고 이중 for문을 이용해 문제를 해결했다.
public class StockPrice {
public static void main(String[] args) {
int[] prices = {1, 2, 3, 2, 3};
int[] result = solution(prices);
for (int r : result) {
System.out.print(r + " ");
}
}
public static int[] solution(int[] prices) {
int[] answer = new int[prices.length];
for (int i = 0; i < prices.length; i++) {
for (int j = i + 1; j < prices.length; j++) {
answer[i]++;
// 가격이 떨어지는 경우 두 번째 for문 종료
if (prices[i] > prices[j]) {
break;
}
}
}
return answer;
}
}
https://github.com/MinchaeKwon/Programmers/blob/master/Level2/src/StockPrice.java