Lintcode - Maximum Subarray III

Given an array of integers and a number k, find k non-overlappingsubarrays which have the largest sum.

The number in each subarray should be contiguous.

Return the largest sum.

http://www.lintcode.com/en/problem/maximum-subarray-iii/

Thought:

一开始以为这和best time to buy and sell stock III 一样的解法,但是test case只过了82%.后来发现问题出在这个必须为K个,而不是at most k.

比如 input 是 [1,-1,-2],k=2. 如果是at most k,那么最后的答案会是1,可是必须是k的话,答案则是0.所以此题并不是每一个数都会走k次,K的最大取值是min(k,i+1),如此我们就能保证在k步时,肯定是包括exactly k个subarray.

此题依然用两个数组维系. 一个是local,一个global. k的更新也依然是从后往前.

dp公式是   local[j] =Math.max(global[i-1],local[j])+nums[i];

global[j]=Math.max(global[j],local[j]);

local维系的是Max(前i-1个数中k-1个subarray最大的sum,i-1个数中k个subarray最大sum,且最后一个subarray一定包括nums[i-1])+当前的数.

时间开销是KN,空间开销是O(K).

  public static int maxSubArray(List<Integer> nums, int k) {
        // write your code
        if(nums==null||nums.size()<k){
            return 0;
        }
        int[] local=new int[k+1];
        int[] global=new int[k+1];
        for(int i=0;i<nums.size();i++){
            //must be min of (k,i+1).otherwise,it is not guaranteed k subarrays,could be less
            for(int j=Math.min(k, i+1);j>0;j--){
                local[j]=nums.get(i)+(j==i+1?global[j-1]:Math.max(global[j-1],local[j]));
                global[j]=j==i+1?local[j]:Math.max(global[j],local[j]);
            }
        }
        return global[k];
    }
时间: 2024-10-13 11:07:32

Lintcode - Maximum Subarray III的相关文章

Lintcode: Maximum Subarray Difference

Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Note The subarray should contain at least one number Example For [1, 2, -3, 1], return 6 Challenge O(n) t

[LintCode] Maximum Subarray 最大子数组

Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarray should contain at least one number. Have you met this question in a real interview? Yes Example Given the array [−2,2,−3,4,−1,2,1,−5,3], the contigu

Lintcode: Maximum Subarray II

Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Note The subarray should contain at least one number Example For given [1, 3, -1, 2,

Maximum Subarray III

Given an array of integers and a number k, find k non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Notice The subarray should contain at least one number Given [-1,4,-2,3,

[Lintcode] Best Time to Buy and Sell Stock I, II, III &amp;&amp; Maximum Subarray

买卖股票系列 && Subarray 相关题目: Best Time to Buy and Sell Stock I && II && III (IV 单独开贴) Maximum Subarray I && II 为什么这些我要总结到一起呢?因为思路基本一致. 题目简略: 买卖股票1: 一次买卖,利润最大. 买卖股票2: N次买卖,利润最大. 买卖股票3: 2次不重叠的买卖,利润最大. Maximum Subarray: Given an a

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

[lintcode medium]Maximum Subarray Difference

Maximum Subarray Difference Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Example For [1, 2, -3, 1], return 6. Note The subarray should contain at leas

Lintcode42 Maximum Subarray II solution 题解

[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The number in each subarray should be contiguous.Return the largest sum. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

[LeetCode][JavaScript]Maximum Subarray

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. https://leetcod