프로그래머스 - 주식가격(C++)

woga·2020년 8월 19일
0

알고리즘

목록 보기
6/26
post-thumbnail

문제 출처: https://programmers.co.kr/learn/courses/30/lessons/42584

문제 난이도

Lv 2


문제 접근법

떨어지지 않는 시간을 구하는 거기 때문에 queue에 값을 놓고, 나머지 주식 가격과 비교한다.


통과 코드

#include <string>
#include <vector>
#include <queue>

using namespace std;

vector<int> solution(vector<int> prices) {
    vector<int> answer;
    queue<pair<int,int>> q;
    for(int i=0; i<prices.size(); i++){
        q.push({i, prices[i]});
    }
    while(!q.empty()){
        int idx = q.front().first;
        int val = q.front().second;
        q.pop();
        int ans = 0;
        for(int i = idx+1; i<prices.size(); i++){
            if(val <= prices[i]) ans++;
            else {
                ans++;
                break;
            }
        }
        answer.push_back(ans);
    }
    return answer;
}
profile
와니와니와니와니 당근당근

0개의 댓글

관련 채용 정보