My Java O(kn) solution


  • 0
    F
    public class Solution {
        public int[] maxSlidingWindow(int[] nums, int k) {
            if(nums == null || nums.length < 1) return new int[]{};
            int[] res = new int[nums.length-k+1];
            Arrays.fill(res, Integer.MIN_VALUE);
            for(int i = 0; i < nums.length; i++){
                for(int j = 0; j < k; j++){
                    if(i-k+1+j < 0 || i-k+1+j >= res.length) continue;
                    res[i-k+1+j] = Math.max(res[i-k+1+j], nums[i]);
                }
            }
            return res;
        }
    }
    

Log in to reply
 

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