Maximum Subarray IV
Problem: Given an integer arrays, find a contiguous subarray which has the largest sum and length should be greater or equal to given length k . Return the largest sum, return 0 if there are fewer than k elements in the array. Analysis: 2/26/2018 update 这道题的坑真多。 1. corner case include when k > nums.length 2. sliding window problem, maxSum's inital value needs to be sum(0, k). 3. Maintain a minSum that k distance away from the current location. Solution: public class Solution { /** * @param nums: an array of integer * @param k: an integer * @return : the largest sum */ public int maxSubarray4 ( int [] nums, int k) { // write your code here if (nums == null || nums . length == 0 || nums . length < k) { return 0 ; } int minSum = 0 , sum = 0 , min = 0 , max = 0 ; for ( int i = 0 ; i < k; i ++ ) { max += nums[i]; } sum = max; ...