【问题标题】:Trying to solve sliding window median problem in leetcodeTrying to solve sliding window median problem in leetcode
【发布时间】:2022-12-19 02:47:07
【问题描述】:

Question: You are given an integer array nums and an integer k. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the median array for each window in the original array. Answers within 10-5 of the actual value will be accepted.

Link to the question: https://leetcode.com/problems/sliding-window-median/description/

currently i'm suspecting that there is a problem in this part of code

const medianSlidingWindow = (array, window) => {
    let start = 0;
    let end = window - 1;
    const min = new MinHeap(array);
    const max = new MaxHeap(array);
    const insert = (index) => {
        if(max.size === 0){
            max.push(index);
            return;
        }
        (array[index] >= max.peak) ? min.push(index) : max.push(index);
        balance();
    }
    const balance = () => {
        if(Math.abs(max.size - min.size) >= 2){
            const returned = (max.size > min.size) ? max.pop() : min.pop();
            (max.size > min.size) ? min.push(returned) : max.push(returned);
        }
    }
    const remove = (index) => {
        (max.has(index)) ? max.pop(index, true) : min.pop(index, true);
        balance();
    }
    const next = () => {
        remove(start++);
        insert(++end);
    }
    const getMedian = () => {
        if(window % 2 === 0) return (max.peak + min.peak)/2;
        return (max.size > min.size) ? max.peak : min.peak;
    }
    for(let i = 0; i <= end; i++){
        insert(i);
    }
    const ret = [];
    while(end < array.length){
        ret.push(getMedian());
        next();
    }
    return ret;
}

link to my full code: https://pastebin.com/fnacFnq4

What went wrong: On the 30th testcase of the problem (link: https://leetcode.com/problems/sliding-window-median/submissions/859041571/), it resolves to a wrong answer but when i pick one of the windows that resolves to a wrong answer it gives me a correct answer, i'm currently confuse because two of the heaps are fairly balanced (as one heap doesn't exceed above one element) and i've tested my heap that both seems to work perfectly. It will be very helpful if somebody helps me. Thank you in advance

Link to SO questions i've followed: How to implement a Median-heap

【问题讨论】:

  • Did you solve this or is it still an open question?

标签: javascript heap median


【解决方案1】:

There are these problems in your heap implementation:

  1. The get function will return null when an out of range index is given, which means the while condition in your sink method could sometimes choose an non-existing child (when there is only one child). Note that a numerical comparison with null will treat that null as 0, and depending of the sign of the value you compare it with can give false or true.

    You can fix this by returning undefined instead of null. Then also make sure that the false side of the comparison operator is the one with +1 (choosing the left child), while the true side takes the other child.

  2. The pop method, when called with true for the second argument, does not guarantee to restore the heap property. It takes care ofsinkingthe value at the given index, but does not consider the case where this value should actuallybubble up!

    Here is a simplified example where I depict a min-heap with thereferencedvalues:

               5
             /   
            8     6
           /    /
         10  12 7
    

    If the node with value 10 is to be removed, the swap action will give this min-heap (which is correct):

               5
             /   
            8     6
           /    /
          7  12 10
    

    And then your code calls sink on that node with value 7. It is clear that there is nothing to sink here, but instead that 7 should bubble up and swap with 8. Your code must foresee both scenarios: sift or bubble.

    If you fix those two issues in your heap implementation, it will work.

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 2022-10-25
    • 2022-12-27
    • 2022-12-01
    • 2022-12-26
    • 2022-12-27
    • 2021-03-09
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多