반갑습니다!

[프로그래머스] 기능개발 본문

알고리즘 문제 풀이

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

김덜덜이 2020. 4. 2. 21:08
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr

풀이

queue에 작업의 indexpush하고 매일매일 작업량을 갱신하며 queue의 앞에서부터 배포 가능한지 확인하면 된다.

코드

#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 n = progresses.size();
    // 큐에 인덱스 저장
    for(int i=0; i<n; i++) q.push(i);

    while(!q.empty()){
        // 날마다 진도율 갱신
        for(int i=0; i<n; i++){
            // 진도율이 100을 넘은 경우는 갱신하지 않음
            if(progresses[i] >= 100) continue;
            progresses[i] += speeds[i];
        }
        int cnt = 0;
        // 앞에서부터 배포 가능한 작업 개수 체크
        while(!q.empty() && progresses[q.front()] >= 100){
            q.pop();
            cnt++;
        }
        if(cnt != 0) answer.push_back(cnt);
    }
    return answer;
}