Skip to content

Commit

Permalink
programmers/level3/heap/disk_controller(디스크컨트롤러)
Browse files Browse the repository at this point in the history
  • Loading branch information
bong6981 committed Jan 22, 2022
1 parent 3091dd1 commit 4c52a83
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
100 changes: 100 additions & 0 deletions programmers/java/src/level3/heap/DiskController.java
@@ -0,0 +1,100 @@
package level3.heap;

import java.util.*;

// https://programmers.co.kr/learn/courses/30/lessons/42627
public class DiskController {
public static void main(String[] args) {
String input = "[[0, 3], [1, 9], [2, 6]]";
input = input.replaceAll("\\[", "{").replaceAll("]", "}");

DiskController dc = new DiskController();
System.out.println(input);
System.out.println(dc.solution(new int[][]{{0, 3}, {1, 9}, {2, 6}}));
}

public int solution(int[][] jobs) {

Arrays.sort(jobs, Comparator.comparing(x -> x[0]));
Queue<Integer[]> queue = new LinkedList<>();

for (int[] job : jobs) {
queue.add(Arrays.stream(job).boxed().toArray(Integer[]::new));
}

PriorityQueue<Integer[]> pq = new PriorityQueue<>(new Comparator<Integer[]>() {
@Override
public int compare(Integer[] integers, Integer[] t1) {
return integers[1] - t1[1];
}
});

pq.offer(queue.poll());
int now = 0;
int wait = 0;

while (!pq.isEmpty()) {
Integer[] job = pq.poll();
int dur = job[1];
int start = job[0];
now = Math.max(now + dur, start + dur);
wait += now - start;
while (!queue.isEmpty() && queue.peek()[0] <= now) {
pq.offer(queue.poll());
}
if (!queue.isEmpty() && pq.isEmpty()) {
pq.offer(queue.poll());
}
}

return wait / jobs.length;


}

// https://programmers.co.kr/learn/courses/30/lessons/42627/solution_groups?language=java
private int solution_other(int[][] jobs) {

Arrays.sort(jobs, new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
if (o1[0] <= o2[0]) {
return -1;
}
return 1;
}
});

PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() {
public int compare(int[] o1, int[] o2) {
if (o1[1] < o2[1]) {
return -1;
}
return 1;
}
});

int time = 0;
int index = 0;
float answer = 0;

while (true) {
while (index < jobs.length && jobs[index][0] <= time) {
queue.offer(jobs[index]);
index++;
}
if (queue.size() == 0) {
time = jobs[index][0];
continue;
}
int[] job = queue.poll();
time += job[1];
answer += time - job[0];
if (index == jobs.length && queue.size() == 0) {
break;
}
}

answer /= jobs.length;
return (int) answer;
}
}
43 changes: 43 additions & 0 deletions programmers/python/level3/heap/disk_controller.py
@@ -0,0 +1,43 @@
import heapq
from collections import deque

def find_jobs_in_time(jobs) :
global time
global pq

while True:
if jobs[0][0] <= time :
job = jobs.popleft()
heapq.heappush(pq, (job[1], job[0]))
if not jobs:
break
else:
break

def solution(jobs):
global idx
global time
global pq

jobs.sort()

n = len(jobs)
pq = []
time = 0
acc_time = 0
jobs = deque(jobs)

while jobs:
find_jobs_in_time(jobs)
if not pq:
time = jobs[0][0]
find_jobs_in_time(jobs)

while pq:
t, s = heapq.heappop(pq)
acc_time += (time - s + t)
time += t
if jobs and time >= jobs[0][0] :
break

return acc_time // n

0 comments on commit 4c52a83

Please sign in to comment.