코딩공작소
[프로그래머스]주식가격 본문
programmers.co.kr/learn/courses/30/lessons/42584
코딩테스트 연습 - 주식가격
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,00
programmers.co.kr
배열의 뒤에서부터 값을 채워나가야겠다는 아이디어로 접근
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class Solution {
public int[] solution(int[] prices) {
int[] answer = new int[prices.length];
answer[prices.length-1] = 0;
for(int i=prices.length-2;i>=0;i--){
int cnt = 0;
for(int j=i+1;j<prices.length;j++){
if(prices[i] <= prices[j]) cnt++;
else {cnt++; break;}
}
answer[i]=cnt;
}
return answer;
}
}
|
cs |
'알고리즘 > 시뮬레이션' 카테고리의 다른 글
[프로그래머스] 호텔 대실 (0) | 2023.11.17 |
---|---|
[프로그래머스] 연속된 부분 수열의 합 (3) | 2023.11.16 |
[프로그래머스]카카오인턴십_크레딧인형뽑기 (0) | 2021.04.20 |
[프로그래머스]가장 큰 수 (0) | 2019.10.25 |
[프로그래머스]기능개발 (0) | 2019.10.24 |