주식 가
문제를 풀기는 풀었지만 n^2으로 풀어서 적어보는 풀이
https://school.programmers.co.kr/learn/courses/30/lessons/42584?language=java (opens in a new tab)
풀이
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int n = prices.length;
int[] answer = new int[n];
ArrayDeque<Integer> stack = new ArrayDeque<Integer>();
for(int i = 0; i < n; i++) {
while(!stack.isEmpty() && prices[stack.peek()] > prices[i]) {
int cur = stack.pop();
answer[cur] = i - cur;
}
stack.push(i);
}
while(!stack.isEmpty()) {
int cur = stack.pop();
answer[cur] = n - cur - 1;
}
return answer;
}
}