LeetCode > 2208 Minimum Operations to Halve Array Sum

2025. 4. 5. 15:12ยท๐Ÿ”’Algorithm

๐Ÿ“Œ๋งํฌ

https://leetcode.com/problems/minimum-operations-to-halve-array-sum/description/

๐Ÿ”จํ’€์ด

์ด ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋ณด๋ฉด ๋‹จ์ˆœํžˆ ์ •๋ ฌ์„ ์ด์šฉํ•ด์„œ ํ’€๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐ๋˜์–ด ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ์—ฐ์‚ฐํšŸ์ˆ˜๋ฅผ ์ตœ์ ํ™” ํ•˜์˜€๋‹ค. ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํž™(์™„์ „์ด์ง„ ํŠธ๋ฆฌ) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜์˜€๊ณ  ์šฐ์„ ์ˆœ์œ„ ํ์—์„œ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๋ฉด์„œ ๊ณ„์‚ฐํšŸ์ˆ˜๋ฅผ ์นด์šดํŒ… ํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํ•ด๋‹น ๋ฌธ์ œ์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O( n log n ) ์ด๋‹ค. ( ์‹œ๊ฐ„๋ณต์žก๋„ ๊ด€๋ก€๋Œ€๋กœ log์˜ ๋ฐ‘์€ ์ƒ๋žต )  

/**
 * @param {number[]} nums
 * @return {number}
 */
var halveArray = function (nums) {
    class MaxHeap {
        constructor() {
            this.heap = [];
        }

        _swap(i, j) {
            let temp = this.heap[i];
            this.heap[i] = this.heap[j];
            this.heap[j] = temp;
        }

        push(n) {
            let heap = this.heap;

            heap.push(n);

            if (heap.length >= 2) {
                let childIdx = heap.length - 1;
                let parentIdx = parseInt((childIdx - 1) / 2);

                while (heap[parentIdx] < heap[childIdx]) {
                    this._swap(parentIdx, childIdx);

                    childIdx = parentIdx;
                    parentIdx = parseInt((childIdx - 1) / 2);

                    if (childIdx === 0) break;
                }
            }
        }

        pop() {
            let q = this.heap;
            const max = q[0];
            q[0] = q.pop();

            if (q.length >= 2) {
                let parentIdx = 0;
                let [leftChildIdx, rightChildIdx] = [parentIdx * 2 + 1, parentIdx * 2 + 2];

                while ((q[leftChildIdx] && q[leftChildIdx] > q[parentIdx]) || (q[rightChildIdx] && q[rightChildIdx] > q[parentIdx])) {
                    let maxChildIdx = leftChildIdx;
                    if (q[rightChildIdx] && q[rightChildIdx] > q[leftChildIdx]) {
                        maxChildIdx = rightChildIdx;
                    }

                    this._swap(parentIdx, maxChildIdx);

                    parentIdx = maxChildIdx;
                    leftChildIdx = parentIdx * 2 + 1;
                    rightChildIdx = parentIdx * 2 + 2;
                }
            }
            return max;
        }
    }
    let sum = nums.reduce((acc, cur) => {
        return acc + cur;
    }, 0);
    let half = sum / 2;
    let totalMinus = 0;
    const maxPriorityQueue = new MaxHeap();
    let cnt = 0;

    nums.forEach((n) => {
        maxPriorityQueue.push(n);
    });

    while (half > totalMinus) {
        let half = maxPriorityQueue.pop() / 2;
        totalMinus += half;
        maxPriorityQueue.push(half);
        ++cnt;
    }

    return cnt;
};

 

'๐Ÿ”’Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

LeetCode [JS] > 859. Buddy strings  (0) 2025.04.05
LeetCode [JS] > 2780. Minimum Index of a Valid Split  (0) 2025.04.05
LeetCode > 1346. Check If N and Its Double Exist  (0) 2025.04.03
LeetCode > 806. Number of Lines To Write String  (0) 2025.04.02
LeetCode > 2022. Convert 1D Array Into 2D Array  (0) 2025.03.29
'๐Ÿ”’Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • LeetCode [JS] > 859. Buddy strings
  • LeetCode [JS] > 2780. Minimum Index of a Valid Split
  • LeetCode > 1346. Check If N and Its Double Exist
  • LeetCode > 806. Number of Lines To Write String
devWarrior
devWarrior
  • devWarrior
    devWarrior
    devWarrior
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๐ŸงฉDev (263)
      • โญFE (34)
      • ๐Ÿ”’Algorithm (155)
      • โž•Etc. (11)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
    • ๊ธ€์“ฐ๊ธฐ
    • ๊ด€๋ฆฌ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ๊ตฌํ˜„
    FE
    nodejs
    ์ž์Šค
    dp
    ๊ทธ๋ฆฌ๋””
    BFS
    ๊ณจ๋“œ5
    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    ์˜ค๋ธ”์™„
    javascript
    react
    ์‹ค๋ฒ„2
    ๋ฐฑ์ค€
    ์‹ค๋ฒ„4
    leetcode
    ํ‹ฐ์Šคํ† ๋ฆฌ์ฑŒ๋ฆฐ์ง€
    ์‹ค๋ฒ„3
    ํ”„๋ก ํŠธ์—”๋“œ
    js
    Easy
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
    Lv2
    ์ฝ”ํ…Œ
    Algorithm
    DFS
    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    ์‹ค๋ฒ„1
    node.js
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
devWarrior
LeetCode > 2208 Minimum Operations to Halve Array Sum
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”