1-4. 프로그래머스
[프로그래머스] 레벨 2 - 주식가격
asdklfjlasdlfkj
2020. 2. 4. 09:56
https://programmers.co.kr/learn/courses/30/lessons/42584
코딩테스트 연습 - 주식가격 | 프로그래머스
초 단위로 기록된 주식가격이 담긴 배열 prices가 매개변수로 주어질 때, 가격이 떨어지지 않은 기간은 몇 초인지를 return 하도록 solution 함수를 완성하세요. 제한사항 prices의 각 가격은 1 이상 10,000 이하인 자연수입니다. prices의 길이는 2 이상 100,000 이하입니다. 입출력 예 prices return [1, 2, 3, 2, 3] [4, 3, 1, 1, 0] 입출력 예 설명 1초 시점의 ₩1은 끝까지 가격이 떨어지지
programmers.co.kr
앞의 요소를 잡고 마지막 요소를 고려해 0을 푸시백해준 뒤, 인덱스 변수에 대한 값의 증감 다음 조건을 확인해주면 바로 뒤의 작아지는 요소에 대응할 수 있다. 간단한 문제.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#include <string>
#include <vector>
using namespace std;
vector<int> solution(vector<int> prices) {
vector<int> answer;
int idx = 0;
for(int i=0; i<prices.size(); i++){
answer.push_back(0);
int cur = prices[i];
for(int j=i+1; j<prices.size(); j++){
answer[idx]++;
if(prices[j] < cur) break;
}
idx++;
}
return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |