import java.util.Stack;
class Solution {
public int[] solution(int[] prices) {
int len = prices.length;
int[] answer = new int[len];
// 각 시간의 인덱스를 stack에 저장하기 위한 용도
Stack<Integer> stack = new Stack<>();
for(int i=0; i < len; i++) {
// 빈 stack이 아니고, 현재 가격이 stack 맨 위 가격보다 작을 때
while(!stack.isEmpty() && prices[i] < prices[stack.peek()]) {
int idx = stack.pop();
answer[idx] = i - idx;
}
stack.push(i);
}
// 가격이 떨어지지 않은 인덱스들 계산
while(!stack.isEmpty()) {
int idx = stack.pop();
answer[idx] = len - 1 - idx;
}
return answer;
}
}
결국 stack으로 못 풀었지만 공부용으로 기록!
인덱스 개념이 0부터라 너무 헷갈린다,,,🥹 일일이 i=0부터 손으로 그려보고 나서야 이해했다,,,,