프로그래머스/Lv.2
코딩테스트 연습 > 주식 가격 - JAVA
아찌방
2024. 1. 10. 22:37
https://school.programmers.co.kr/learn/courses/30/lessons/42584
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이 - 나의 생각
https://fall-in-dream.tistory.com/72
코딩테스트 연습 > 뒤에 있는 큰 수 찾기 - JAVA
https://school.programmers.co.kr/learn/courses/30/lessons/154539 풀이 - 나의 생각 뒤에 있는 나보다 큰 수를 찾는 것이기 때문에 뒤에서부터 찾으면 수월할 거라고 생각을 했다. 최악의 경우 1, 1, 1 , 1 , 1 , 1 , 1 ,
fall-in-dream.tistory.com
이 문제와 똑같다!
코드
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int len = prices.length, index = 0;
int[] answer = new int[len];
Deque<Integer> stack = new ArrayDeque<>();
stack.offer(len-1);
answer[len-1] = 0;
for(int i = len - 2; i >= 0; i--){
while(!stack.isEmpty()){
index = stack.peekLast();
if(prices[i] > prices[index]){
answer[i] = index - i;
break;
}else{
stack.pollLast();
}
}
if(stack.isEmpty()){
answer[i] = len - i - 1;
}
stack.offer(i);
}
return answer;
}
}
728x90