What is the problem in the code?? Stuck on last test case


  • 1
    S
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int* maxSlidingWindow(int* nums, int numsSize, int k, int* returnSize) {
        if(k==0 || numsSize ==0)
            return NULL;
        int * window = (int *)malloc((4*k) * sizeof(int));
        int * result = (int *)malloc(numsSize *sizeof(int));
        int i, index=0, w=-1, front =-1;
        window[++w] = 0;
        for(i=1; i<k; i++){
            while(w != front && nums[i] >= nums[window[w]])
                w--;
            window[++w] = i;
        }
        result[index++] = nums[window[front+1]];
        for(; i<numsSize; i++){
            while(front != w && window[front+1] < i-k+1)
                front++;
            while( w!= front && (nums[i] >= nums[window[w]] || window[w] < i-k+1))
                w--;
            window[++w] = i;
            result[index++] = nums[window[front+1]];
        }
        *returnSize = index;
        return result;
    }

  • 0
    S

    @sajalgarg do u know why now?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.