Simple Java Solution


  • 0
    Z
    public class Solution {
        public int getMax(int[] nums, int start, int end) {
            int max = Integer.MIN_VALUE;
            for (int i = start; i < end; i++) {
                max = Math.max(max, nums[i]);
            }
            return max;
        }
    
        public int[] maxSlidingWindow(int[] nums, int k) {
            if (nums == null || nums.length == 0 || k == 0) {
                return new int[0];
            }
            if (nums.length <= k) {
                int max = Integer.MIN_VALUE;
                for (int num : nums) {
                    max = Math.max(max, num);
                }
                return new int[]{max};
            }
    
            int size = nums.length - k + 1;
            int[] result = new int[size];
            for (int i = 0; i < size; i++) {
                result[i] = getMax(nums, i, i + k);
            }
            return result;
        }
    }
    

  • 0
    T

    Nice simple solution. But, Is this O(n^2) ??


Log in to reply
 

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