코딩테스트/프로그래머스

[프로그래머스] 주식가격

하이하이루루 2023. 7. 7. 12:24

프로그래머스 주식가격 C++ 풀이

 

https://programmers.co.kr/learn/courses/30/lessons/42584

 

 

// 버블 소트 방식으로 이용할꺼 백트래킹 안하면 솔직히 시간초과 날듯
// 비교하고 자기보다 작은거 나오면 바로 break
// 그리고 마지막에 비교 안된값(마지막값)은 0이니까 push_back해줌
#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 cnt = 0;
        for (int j = i+1; j < prices.size(); j++){
            cnt++;
            if (prices[i] > prices[j]) // 백트랙킹 안하면 시간초과 날듯
                break;
        }
        answer.push_back(cnt);
    }
    answer.push_back(0); // 마지막 원소의 경우
    return answer;
}