Intuitive One Pass Sliding Window Solution with no Extra space


  • 0
    T

    Hello, this is my solution.

    public int[] maxSlidingWindow(int[] nums, int k) {
    ArrayList<Integer> list = new ArrayList<Integer>();

        int max = Integer.MIN_VALUE;
        if(nums.length == 0){
            int[] res = {};
            return res;
        }
        for(int i = 0; i < nums.length - k + 1;i++){
            for(int j = i; j < i+k; j++){
                max = Math.max(max,nums[j]);
            }
            list.add(max);
            max = Integer.MIN_VALUE;
        }
        
        int[] res = new int[list.size()];
        for(int i = 0; i < list.size(); i++) res[i] = list.get(i);
        return res;
    }

Log in to reply
 

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