728x90
반응형
https://programmers.co.kr/learn/courses/30/lessons/42584?language=java
- 이중 반복문 사용
class Solution {
public 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]++;
if(prices[i] > prices[j]){
break;
}
}
}
return answer;
}
}
- stack 사용
import java.util.*;
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
Stack<Integer> stack = new Stack();
for(int i=0; i<prices.length; i++){
while(!stack.isEmpty() && prices[i] < prices[stack.peek()]){
answer[stack.peek()] = i - stack.peek();
stack.pop();
}
stack.push(i);
}
while(!stack.isEmpty()){
answer[stack.peek()] = prices.length - stack.peek() - 1;
stack.pop();
}
return answer;
}
}
참고
https://girawhale.tistory.com/7
728x90
반응형
'알고리즘' 카테고리의 다른 글
[프로그래머스/Java] 이중우선순위큐 (0) | 2022.01.10 |
---|---|
[프로그래머스/JAVA] 더 맵게 (0) | 2021.12.30 |
[프로그래머스/JAVA] 기능개발(스택/큐) (0) | 2021.12.27 |
[재귀함수/backtracking] n queens problem (0) | 2021.12.02 |
[재귀함수] Counting Cells in a Blob (0) | 2021.11.30 |