https://programmers.co.kr/learn/courses/30/lessons/42584?language=cpp

 

코딩테스트 연습 - 주식가격

초 단위로 기록된 주식가격이 담긴 배열 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
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <vector>
 
using namespace std;
 
vector<int> solution(vector<int> prices) {
    vector<int> answer;
    for (int i = 0; i < prices.size()-1; i++)
    {
        int count = 0;
        for (int j = i + 1; j < prices.size(); j++)
        {
            if (prices[i] <= prices[j]) //값이 떨어지지 않았으면 시간++
            {
                count++;
            }
            else //떨어졌다면 시간++ 하고 for문을 빠져나온다.
            {
                count++;
                break;
            }
        }
        answer.push_back(count);
    }
    
    return answer;
}

 

+ Recent posts