๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

42627. ๋””์Šคํฌ ์ปจํŠธ๋กค๋Ÿฌ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋””์Šคํฌ ์ปจํŠธ๋กค๋Ÿฌ


๋ฌธ์ œ ์œ ํ˜•๋‚œ์ด๋„๊ฑธ๋ฆฐ ์‹œ๊ฐ„ํ•ด๊ฒฐ ์œ ๋ฌด(โœ…/โŒ)
ํž™lv.31์‹œ๊ฐ„โœ…

์„ค๊ณ„ ๋ฐฉ๋ฒ•#

  • jobs ๋ฅผ ์‹œ๊ฐ„์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ํ์— ๋„ฃ๋Š”๋‹ค.

  • ํ˜„์žฌ ์‹œ๊ฐ„ time ์ด๋‚ด์— ์žˆ๋Š” job ๋“ค์„ ๋ชจ๋‘ heap ์— ๋„ฃ๋Š”๋‹ค.

  • ํž™์—์„œ ์ตœ์†Œ ์‹œ๊ฐ„ job ์„ ๊บผ๋‚ธ๋‹ค.

  • ํ˜„์žฌ job์˜ ๋ฐ˜ํ™˜์‹œ๊ฐ„(ํ˜„์žฌ์‹œ๊ฐ„ - job ์š”์ฒญ ์‹œ๊ฐ„ + job ์ˆ˜ํ–‰ ์‹œ๊ฐ„)์„ ์ด ๋ฐ˜ํ™˜์‹œ๊ฐ„์— ๋”ํ•œ๋‹ค.

  • ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

  • job ์ด ์—†์„ ๊ฒฝ์šฐ time ์„ 1๋งŒํผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

์ฝ”๋“œ#

function solution(jobs) {    const heap = new MinHeap();    const done = [];
    jobs.sort((a, b) => a[0] - b[0]);
    const jobQueue = jobs.reduce((queue, job) => {        queue.enqueue(job);        return queue;    }, new Queue());
    let turnaroundTime = 0;    let time = 0;    let i = 0;    while (done.length !== jobs.length) {        while (!jobQueue.isEmpty() && jobQueue.peek()[0] <= time) {            heap.insert(jobQueue.dequeue());        }
        const currentJob = heap.remove();        if (currentJob) {            turnaroundTime += time - currentJob[0] + currentJob[1];            time += currentJob[1];            done.push(currentJob);        } else {            time++;        }    }
    return Math.floor(turnaroundTime / jobs.length);}
class Queue {    constructor() {        this._items = [];        this.start = 0;        this.end = 0;    }
    enqueue(item) {        this._items.push(item);        this.end += 1;    }
    dequeue() {        return this._items[this.start++];    }
    peek() {        return this._items[this.start];    }
    size() {        return this.end - this.start;    }
    isEmpty() {        return this.end === this.start;    }}
class MinHeap {    constructor() {        this.heap = [null];    }
    getMin() {        return this.heap[1];    }
    size() {        return this.heap.length - 1;    }
    insert(node) {        this.heap.push(node);
        if (this.heap.length > 1) {            let current = this.heap.length - 1;
            while (                current > 1 &&                this.heap[Math.floor(current / 2)][1] > this.heap[current][1]            ) {                [this.heap[Math.floor(current / 2)], this.heap[current]] = [                    this.heap[current],                    this.heap[Math.floor(current / 2)],                ];                current = Math.floor(current / 2);            }        }    }
    remove() {        let smallest = this.heap[1];
        if (this.heap.length > 2) {            this.heap[1] = this.heap[this.heap.length - 1];            this.heap.splice(this.heap.length - 1);
            if (this.heap.length === 3) {                if (this.heap[1][1] > this.heap[2][1]) {                    [this.heap[1], this.heap[2]] = [this.heap[2], this.heap[1]];                }                return smallest;            }
            let current = 1;            let leftChildIndex = current * 2;            let rightChildIndex = current * 2 + 1;
            while (                this.heap[leftChildIndex] &&                this.heap[rightChildIndex] &&                (this.heap[current][1] > this.heap[leftChildIndex][1] ||                    this.heap[current][1] > this.heap[rightChildIndex][1])            ) {                if (this.heap[leftChildIndex][1] < this.heap[rightChildIndex][1]) {                    [this.heap[current], this.heap[leftChildIndex]] = [                        this.heap[leftChildIndex],                        this.heap[current],                    ];                    current = leftChildIndex;                } else {                    [this.heap[current], this.heap[rightChildIndex]] = [                        this.heap[rightChildIndex],                        this.heap[current],                    ];                    current = rightChildIndex;                }
                leftChildIndex = current * 2;                rightChildIndex = current * 2 + 1;            }        } else if (this.heap.length === 2) {            this.heap.splice(1);        } else {            return null;        }
        return smallest;    }}

์‹œ๊ฐ„ ๋ณต์žก๋„

  • jobs ์ •๋ ฌ : O(NlogN)

  • job ์‚ฝ์ž… ๋ฐ ์กฐํšŒ : O(logN) * N

์–ด๋ ค์› ๋˜ ์ #

  • heap์˜ ๊ธฐ์ค€์ด ๋  job ์˜ ์ˆ˜ํ–‰์‹œ๊ฐ„(๋ฐฐ์—ด์˜ [1] ์š”์†Œ)์— ๋”ฐ๋ผ heap์˜ ๋กœ์ง์„ ์ˆ˜์ •ํ•ด์•ผ ํ–ˆ์Œ.

  • heap์— ๋„ฃ์„ ๋…ธ๋“œ๋ฅผ ์ž˜ ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜๊ฒŒ ๋จ.

์ฐธ๊ณ ์ž๋ฃŒ#