My java solution without queue


  • 0
    S

    public int[] maxSlidingWindow(int[] nums, int k) {
    if(nums.length == 0) return new int[0];
    int[] res = new int[nums.length - k + 1];
    int left =0, right = 0, max = Integer.MIN_VALUE, index = 0;
    while(left <= right && right < nums.length) {
    while(right < nums.length && right - left < k) {
    if(nums[right] >= max) {
    max = nums[right];
    index = right;
    }
    right++;
    }
    if(right - left < k) break;
    res[left] = max;
    if(left == index) {
    max = Integer.MIN_VALUE;
    right = left + 1;
    }
    left++;
    }

        return res;
    }

Log in to reply
 

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