Simple Java idea, easy understand


  • 0
    F

    Not best, but easily get point.

        public class Solution {
        public int[] maxSlidingWindow(int[] nums, int k) {
            if (nums == null || nums.length == 1 || nums.length == 0) return nums;
            int length = nums.length - k + 1;
            int[] result = new int[length];
    
            int start = 0;
            int end = k - 1;
    
            int index = 0;
            for (int i = k - 1; i < nums.length; i++) {
                int max = nums[i];
                for (int j = start; j <= i; j++) {
                    max = Math.max(max, nums[j]);
                }
                result[index] = max;
                index++;
                start++;
            }
    
            return result;    
        }    
    }

Log in to reply
 

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