Lv2. 디스크 컨트롤러 Javascript
https://programmers.co.kr/learn/courses/30/lessons/42627
function solution(jobs) {
jobs.sort((a, b) => {
return a[0] === b[0]
? a[1] - b[1]
: a[0] - b[0];
})
let sum = 0, count = 0, now = 0;
while (jobs.length > 0) {
const tasks = jobs.filter(item => item[0] <= now)
let pop
if (tasks.length == 1) {
const index = jobs.indexOf(tasks[0])
pop = jobs.splice(index, 1)[0]
} else if (tasks.length > 1) {
const times = tasks.map(task => task[1])
const minValue = Math.min(...times)
const minIndex = times.indexOf(minValue)
const index = jobs.indexOf(tasks[minIndex])
pop = jobs.splice(index, 1)[0]
} else {
pop = jobs.shift()
}
console.log(pop);
sum += pop[0] >= now ? pop[1] : pop[1] + now - pop[0]
now = pop[0] >= now ? pop[0] + pop[1] : now + pop[1]
count++
}
return Math.floor(sum / count)
}
const jobs = [[0, 3], [1, 9], [2, 6]]
console.log(
solution(jobs)
);
function solution(jobs) {
jobs.sort((a, b) => {
return a[0] === b[0]
? a[1] - b[1]
: a[0] - b[0];
})
let sum = 0, count = 0, now = 0;
while (jobs.length > 0) {
const tasks = jobs.filter(item => item[0] <= now)
let pop
if (tasks.length == 1) {
const index = jobs.indexOf(tasks[0])
pop = jobs.splice(index, 1)[0]
} else if (tasks.length > 1) {
const times = tasks.map(task => task[1])
const minValue = Math.min(...times)
const minIndex = times.indexOf(minValue)
const index = jobs.indexOf(tasks[minIndex])
pop = jobs.splice(index, 1)[0]
} else {
pop = jobs.shift()
}
console.log(pop);
sum += pop[0] >= now ? pop[1] : pop[1] + now - pop[0]
now = pop[0] >= now ? pop[0] + pop[1] : now + pop[1]
count++
}
return Math.floor(sum / count)
}
const jobs = [[0, 3], [1, 9], [2, 6]]
console.log(
solution(jobs)
);
참고 Ref: [Algorithm] 알고리즘 - 디스크 컨트롤러 (힙 - Heap )
댓글 환영
질문 환영
by.protect-me