관리 메뉴

fatalite

C++ 주식 가격 [고득점 Kit 스택 / 큐] 본문

코딩 인터뷰/프로그래머스

C++ 주식 가격 [고득점 Kit 스택 / 큐]

fataliteforu 2023. 5. 13. 14:04

Problem

난이도: 프로그래머스 2단계

분류: 스택이라는데,, set과 pair 써서 풀었다. 

for문쓰면서 erase 할 생각하지말자. 이럴거면 vector써도 똑같았을듯?
아무튼 list를 두자는 아이디어는 괜찮았던듯.

Solution

#include <string>
#include <vector>
#include <iostream>
#include <set>
using namespace std;

vector<int> solution(vector<int> prices) {
    vector<int> answer;
    set<pair<int,int>> List;
    answer.resize(prices.size(),0);
    for(int i = 0; i < prices.size()-1; i++){
        List.insert({i, prices[i]});
        set<pair<int,int>> ListCopy = List;
        for(pair<int,int> p : ListCopy){
            if(p.second <= prices[i]){
                answer[p.first]++;
            }else{
                List.erase(p);
            }
        }
    }
    return answer;
}