알고리즘/프로그래머스
뒤에 있는 큰 수 찾기
베리영young
2025. 1. 28. 20:23
사용 알고리즘: 자료구조-스택
사용 언어: java
import java.util.*;
class Solution {
public int[] solution(int[] numbers) {
int[] answer = new int[numbers.length];
Arrays.fill(answer, -1);
Stack<Integer> indexStack = new Stack<>();
for(int i=0; i<numbers.length; i++) {
if(indexStack.isEmpty()) {
indexStack.push(i);
continue;
}
while(!indexStack.isEmpty()) {
if(numbers[indexStack.peek()] >= numbers[i])
break;
answer[indexStack.pop()] = numbers[i];
}
indexStack.push(i);
}
return answer;
}
}
뭔지 모를 때는
값이 아니라 인덱스를 갖고 놀아봐라~