문제
문제풀이
#include <vector>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct cmp {
bool operator()(vector<int> a, vector<int> b) {
return a.at(1) > b.at(1);
}
};
int solution(vector<vector<int>> jobs) {
int answer = 0;
priority_queue<vector<int>, vector<vector<int>>, cmp> pq; //우선순위 큐 min heap
int size = jobs.size();
int time=0;
//이차원배열 정렬하기
sort(jobs.begin(), jobs.end());
int idx = 0;int j=0;
while(idx < size || !pq.empty()){
//현재 시간보다 이하에 들어온 작업들을 모두 넣는다.
if( idx < size && time >= jobs[idx][0] ){//&&여기 순서 틀리면 틀림..
pq.push(jobs[idx++]);
continue;//아래 작업하지 말고 다시 확인
//같은 시간대에 다른 작업이 들어올수 있으므로 다시 확인한다
}
//현재 시간보다 아직 안 들어온 경우 idx 순서에서.
if(!pq.empty()){
// answer += time - pq.top()[0] + pq.top()[1];//대기 시간?
// time += pq.top()[1];
time += pq.top()[1];
//작업시간에 대기 시간만큼 추가(현재시간 - 들어온 시간)
answer += time - pq.top()[0];
pq.pop();
}
else
time = jobs[idx][0];
}
return answer/size;
}
'programming language > Structure' 카테고리의 다른 글
[프로그래머스 Level1 해시] 완주하지 못한 선수 (0) | 2021.08.05 |
---|---|
Hash 개념 정리, std::unordered_map (0) | 2021.08.05 |
[프로그래머스 Level3 그래프] 가장 먼 노드 (0) | 2021.08.01 |
[프로그래머스 Level2 힙] 더 맵게 (0) | 2021.07.30 |
[백준 스택문제] 9021번 괄호 (0) | 2021.07.30 |