[프로그래머스 / C++] 디스크 컨트롤러

Seulguo·2022년 7월 7일
0

Algorithm

목록 보기
21/185
post-thumbnail
post-custom-banner

🐣 문제

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/42627


🐥 코드

#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;

struct compare {
    bool operator()(vector<int> a, vector<int> b) {
        return a[1] > b[1];
    }
};

int solution(vector<vector<int>> jobs) {
    int answer = 0;
    int i = 0;
    int time = 0;
    priority_queue<vector<int>, vector<vector<int>>, compare> pq;
    sort(jobs.begin(), jobs.end());
    while(i < jobs.size() || !pq.empty()){
        if(i < jobs.size() && time >= jobs[i][0]){
            pq.push(jobs[i++]);
            continue;
        }    
        if(!pq.empty()){
            time += pq.top()[1];
            answer += (time - pq.top()[0]);
            pq.pop();
        }
        else{
            time = jobs[i][0];
        }
    }
     
    return answer/jobs.size();
}
post-custom-banner

0개의 댓글