0

Can anyone walk me through this solution below? What does p mean? why its range j-1 to i? Thanks 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.

according to this blog(http://www.cnblogs.com/lishiblog/p/4183917.html), the DP analysis is

DP. d[i][j] means the maximum sum we can get by selecting j subarrays from the first i elements.

d[i][j] = max{d[p][j-1]+maxSubArray(p+1,i)}

we iterate p from i-1 to j-1, so we can record the max subarray we get at current p, this value can be used to calculate the max subarray from p-1 to i when p becomes p-1.

public class Solution {
/**
 * @param nums: A list of integers
 * @param k: An integer denote to find k non-overlapping subarrays
 * @return: An integer denote the sum of max k non-overlapping subarrays
 */
public int maxSubArray(ArrayList<Integer> nums, int k) {
    if (nums.size()<k) return 0;
    int len = nums.size();
    //d[i][j]: select j subarrays from the first i elements, the max sum we can get.
    int[][] d = new int[len+1][k+1];
    for (int i=0;i<=len;i++) d[i][0] = 0;        

    for (int j=1;j<=k;j++)
        for (int i=j;i<=len;i++){
            d[i][j] = Integer.MIN_VALUE;
            //Initial value of endMax and max should be taken care very very carefully.
            int endMax = 0;
            int max = Integer.MIN_VALUE;                
            for (int p=i-1;p>=j-1;p--){
                endMax = Math.max(nums.get(p), endMax+nums.get(p));
                max = Math.max(endMax,max);
                if (d[i][j]<d[p][j-1]+max)
                    d[i][j] = d[p][j-1]+max;                    
            }
        }

    return d[len][k];


}

}

1 Answer 1

1

What does p mean: just a iterator. (Chinese Algorithm Coder always like short name for variable...)

Why its range j-1 to i:

Indeed, dp analysis should be:

d[i][j] = max{d[p][j-1]+maxSubArray(p+1,i)} j-1 <= p <= i-1

Why must p >= j-1? Because as dp[i][j] define:

d[i][j] means the maximum sum we can get by selecting j subarrays from the first i elements.

You know , we can't select j sub-array without non-overlapping from j-1 elements. That is to say, dp[i][j] make sense when i >= j.

Any question, leave a comment here.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much, Sayakiss;-) Now I understand the range for p. I am still confused at what endMax and max stands for. Also, why we get: d[i][j] = max{d[p][j-1]+maxSubArray(p+1,i)} with p's range as j-1 to i-1. So d[i][j] means max sum by selecting j subarray from first i elements, so d[p][j-1]means max sum by selecting j-1 subarray from first p element, then why we need to add maxSubArray(p+1, i)? In fact, I am confused at maxSubArray(p+1,i), does it mean from this Array[p+1...to len], find i non-overlapping subarray? Thanks;-)
max and endMax is about another question: How to find just one max subarray in O(n) Time? So do you have any idea about that? I think it's good try it yourself...
oh~I suddenly realize why I am stuck! Thanks @Sayakiss
@YuanVivienWang Welcome. You think it out by yourself, congratulations!
@ Sayakiss, not without your kind help;-) Thanks again!~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.