코딩테스트

[C++] 프로그래머스 - 기능개발

goliot 2024. 4. 15. 02:58
반응형

 

- 어차피 순서대로 도니까 큐가 편할 듯 하다

- 큐에 인덱스를 저장해두고, 해당 processes[q.top()]이 100보다 크면 카운터를 올려준다

- 카운터가 0이 아니면, 뭔가 완료됐다는 뜻이므로, answer에 넣는다

 

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

using namespace std;

vector<int> solution(vector<int> progresses, vector<int> speeds) {
    vector<int> answer;
    queue<int> q;
    int size = progresses.size();
    for(int i=0; i<size; i++) {
        q.push(i);
    }
    
    while(!q.empty()) {
        int cnt = 0;
        for(int i=0; i<size; i++) {
            progresses[i] += speeds[i];
        }
        while(!q.empty() && progresses[q.front()] >= 100) {
            cnt++;
            q.pop();
        }
        if(cnt != 0) answer.push_back(cnt);
    }
    
    return answer;
}

- 처음에는 또 어렵게 생각했었다

- pair<pair<int, int> int> 이렇게 모든 정보와 인덱스까지 저장하려 했었는데 하등 쓸모없는 짓이었다.

- 언제쯤 문제를 쉽게 생각할까

반응형