Java8 functional style solution


  • 4
    D

    public int[] maxSlidingWindow(int[] nums, int k) {

        if(nums.length == 0) return nums;
    
        int[] arr = IntStream.range(0, nums.length - k + 1).map(i ->
            Arrays.stream(Arrays.copyOfRange(nums, i, i + k)).reduce(Integer.MIN_VALUE, Math::max)
        ).toArray();
    
        return arr;
    

    }


Log in to reply
 

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