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

42628. ์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ


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

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

  • minHeap๊ณผ ์Œ์ˆ˜๋กœ ์ €์žฅํ•˜๋Š” maxHeap์„ ๋™์‹œ์— ์‚ฌ์šฉ.

  • length ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋‘๊ณ , length๊ฐ€ 1์ผ ๋•Œ D op๋ฅผ ๋ฐ›์œผ๋ฉด ๋‘ ํž™์„ ๋ชจ๋‘ ์ดˆ๊ธฐํ™” ์‹œ์ผœ๋ฒ„๋ฆผ.

  • ๊ฒฐ๊ณผ๋ฅผ ์ œ์ถœํ•  ๋•Œ์—๋„ length๊ฐ€ 0์ด๋ฉด [0, 0]์„ ๋ฆฌํ„ดํ•˜๊ฒŒ ๋” ๋ฐ”๊พผ๋‹ค.

์ฝ”๋“œ#

function solution(operations) {    const minHeap = new MinHeap();    const maxHeap = new MinHeap();    let length = 0;
    operations.forEach((op) => {        const [opcode, num] = op.split(' ');
        if (opcode === 'I') {            minHeap.insert(parseInt(num));            maxHeap.insert(-parseInt(num));            length++;        } else if (length === 0) {        } else if (length === 1) {            minHeap.reset();            maxHeap.reset();            length = 0;        } else if (num === '1') {            maxHeap.remove();            length--;        } else if (num === '-1') {            minHeap.remove();            length--;        }    });
    if (!length) {        return [0, 0];    } else {        return [-maxHeap.remove(), minHeap.remove()];    }}
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] !== undefined &&                this.heap[rightChildIndex] !== undefined &&                (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;    }
    reset() {        this.heap = [null];    }}

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

O(NlogN)

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

  • JS์—์„œ ํž™์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ, ์ด ํž™ ์ž์ฒด๊ฐ€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.

  • 0์ด ๋…ธ๋“œ๋กœ ๋“ค์–ด๊ฐ”์„ ๋–„ 0์ธ ๋…ธ๋“œ๋ฅผ null๋กœ ํŒ๋‹จํ•ด์„œ remove, insert ์—ฐ์‚ฐ์‹œ ์ด ๋…ธ๋“œ์— ๋Œ€ํ•ด ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์•˜๊ณ , ์ด๋ฅผ ์ฐพ๋Š”๋ฐ ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

  • ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํž™์„ ์ˆ˜์ •ํ•ด์„œ ํ†ต๊ณผํ–ˆ๋Š”๋ฐ, ์—ญ์‹œ๋‚˜ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์™€์„œ ์“ฐ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ์ง์ ‘ ๊ตฌํ˜„ํ•˜๋‹ค๋ณด๋‹ˆ ์–ธ์ œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ• ์ง€ ๋ชจ๋ฅธ๋‹ค๋Š” ์–ด๋ ค์›€์ด ์žˆ๋‹ค.

while (  this.heap[leftChildIndex] !== undefined &&  this.heap[rightChildIndex] !== undefined &&  (this.heap[current] > this.heap[leftChildIndex] ||  this.heap[current] > this.heap[rightChildIndex]))