-
프로그래머스 - 디스크 컨트롤러알고리즘 풀이/프로그래머스 2019. 12. 1. 23:18
문제 : https://programmers.co.kr/learn/courses/30/lessons/42627
코딩테스트 연습 - 디스크 컨트롤러 | 프로그래머스
하드디스크는 한 번에 하나의 작업만 수행할 수 있습니다. 디스크 컨트롤러를 구현하는 방법은 여러 가지가 있습니다. 가장 일반적인 방법은 요청이 들어온 순서대로 처리하는 것입니다. 예를들어 - 0ms 시점에 3ms가 소요되는 A작업 요청 - 1ms 시점에 9ms가 소요되는 B작업 요청 - 2ms 시점에 6ms가 소요되는 C작업 요청 와 같은 요청이 들어왔습니다. 이를 그림으로 표현하면 아래와 같습니다. 한 번에 하나의 요청만을 수행할 수 있기 때문에 각각의
programmers.co.kr
풀이 :
jobs들을 시간 순서대로 정렬한다.
현재 시간 내에 올 수 있는 jobs들을 모두 pq에 넣는다. 그 후 pq가 빌 때까지 다 더해주고
pq가 비어있는 상태라면 다음 시간에 있는 jobs 하나를 선택한다.
(하드디스크가 작업을 수행하고 있지 않을 때에는 먼저 요청이 들어온 작업부터 처리합니다에 해당)
코드 ( C ++ )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters#include <iostream> #include <string> #include <vector> #include <queue> #include <algorithm> using namespace std; int solution(vector<vector<int>> jobs) { int answer = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; sort(jobs.begin(), jobs.end()); int idx = 0; int time = 0; int n = jobs.size(); while (idx < n || !pq.empty()) { while (idx < n && jobs[idx][0] <= time) { pq.push({ jobs[idx][1], jobs[idx][0] }); idx++; } if (!pq.empty()) { time += pq.top().first; answer += time - pq.top().second; pq.pop(); } else time = jobs[idx][0]; } answer /= jobs.size(); return answer; } int main() { cout << solution({ {0,3},{4,7},{12,15 } }) << "\n"; return 0; } '알고리즘 풀이 > 프로그래머스' 카테고리의 다른 글
프로그래머스 - 위장 (0) 2019.12.05 프로그래머스 - 탑 (0) 2019.12.03 프로그래머스 - 가장 큰 수 (0) 2019.12.01 프로그래머스 - 라면공장 (0) 2019.12.01 프로그래머스 - 프린터 (0) 2019.12.01