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

42626. ๋” ๋งต๊ฒŒ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋” ๋งต๊ฒŒ


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

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

  • ํž™์„ ๊ตฌ์„ฑํ•˜๊ณ  ํž™์˜ ๋ฃจํŠธ(์ตœ์†Œ๊ฐ’)์ด K๋ณด๋‹ค ํด ๋•Œ๊นŒ์ง€ ์š”๊ตฌ์‚ฌํ•ญ๋Œ€๋กœ (์ตœ์†Œ๊ฐ’ + 2๋ฒˆ์งธ์ตœ์†Œ๊ฐ’ * 2)๋ฅผ ํž™์— ๋„ฃ๋Š”๋‹ค.

  • ํž™์˜ ๊ธธ์ด๊ฐ€ 2๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ -1์„ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•œ๋‹ค .

์ฝ”๋“œ#

ํŒŒ์ด์ฌ#

import heapq

def solution(scoville, K):    heapq.heapify(scoville)    count = 0
    while scoville[0] < K:        if len(scoville) < 2:            return -1
        heapq.heappush(scoville, heapq.heappop(scoville) + heapq.heappop(scoville) * 2)        count += 1
    return count

JS#

function solution(scoville, K) {    let count = 0;    const heap = scoville.reduce((heap, food) => {        heap.insert(food);        return heap;    }, new MinHeap());
    while (heap.getMin() < K) {        if (heap.size() < 2) {            return -1;        }
        heap.insert(heap.remove() + heap.remove() * 2);        count += 1;    }
    return count;}
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)] > this.heap[current]            ) {                [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] > this.heap[2]) {                    [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] > this.heap[leftChildIndex] ||                    this.heap[current] > this.heap[rightChildIndex])            ) {                if (this.heap[leftChildIndex] < this.heap[rightChildIndex]) {                    [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;    }}

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

  • ํž™ ๊ตฌ์„ฑ : O(NlogN)

  • ํž™ ์ˆœํšŒ : O(N) (์ตœ์†Œ๊ฐ’ ์กฐํšŒ O(1)์— ๊ฐ€๋Šฅ)

  • O(NlogN)

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

  • ํŒŒ์ด์ฌ์˜ heapq ๋ชจ๋“ˆ์ด ๋„ˆ๋ฌด ํŽธํ–ˆ๋‹ค.

  • ๋ฐ˜๋Œ€๋กœ JS์—์„œ ํž™์„ ์“ฐ๊ธฐ ๋„ˆ๋ฌด ๋ถˆํŽธํ–ˆ๋‹ค.

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

heapq --- ํž™ ํ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ํŒŒ์ด์ฌ ์„ค๋ช…์„œ ์ฃผ์„ํŒ

[ํŒŒ์ด์ฌ] heapq ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฒ•

Collections for JavaScript