[Leetcode] Split Array Largest Sum

Posted 自在时刻

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Leetcode] Split Array Largest Sum相关的知识,希望对你有一定的参考价值。

Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays.
Note:
If n is the length of array, assume the following constraints are satisfied:
1 ≤ n ≤ 1000
1 ≤ m ≤ min(50, n)
Examples:
Input:
nums = [7,2,5,10,8]
m = 2
Output:
18
Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.

这道题目,可以采用递归,那么思路如下:

dfs(int m,int i)
    if(边界条件) ...
    int min = Integer.MAX_VALUE:
    for(int j = i + 1; j < len; j++)
        max = Math.max(sum(i,j), dfs(m+1, j));
        min = Math.min(min,max);
    
    return min;

m表示当前切了几次,i表示从哪个元素开始切。改成dp以后,可以采用dp[m][i] 表示从i开始一直到结尾的数组分成m组每组和最大值最小。
从而有一个递推,将i以后切成m段,这m段实机包含一个sum(i,j) 和dp[m-1][j] ,也就是一段加上 m-1段。找出所有情况的最小值。
dp[m][i] = min max sum[i][j], dp[m-1][j]。
实现的时候,一个要注意每一片都不能为空,所以i和j的最大值有界限。同时可以对数组进行预处理,获得一个累加和数组sum,从而 nums[i] + nums[i+1]…+nums[j] = sum[j] - sum[i]。


public class Solution 
    public int splitArray(int[] nums, int m) 
        int[][] dp = new int[m][nums.length];
        int len = nums.length;
        int[] sum = new int[len + 1];
        sum[0] = 0;
        for (int i = 0; i < len; i++) 
            sum[i + 1] = sum[i] + nums[i];
        
        for(int i=0; i<len; i++)
            dp[0][i] = sum[len]-sum[i];

        for (int k = 1; k < m; k++) 
            for (int i = 0;i < len - k;i ++) 
                dp[k][i] =  Integer.MAX_VALUE;
                for (int j = i + 1; j <= len - k;j++) 
                    dp[k][i] = Math.min(dp[k][i], Math.max(sum[j] - sum[i], dp[k - 1][j]));
                
            

        
        return dp[m - 1][0];

    

dp的空间可以进一步优化。因为我们改变的只是一个dp[k][i]的值,而使用的是它上一行i后面的元素,所以可以直接优化进一个一维数组中。代码如下:

public class Solution 
    public int splitArray(int[] nums, int m) 
        int[] dp = new int[nums.length];
        int len = nums.length;
        int[] sum = new int[len + 1];
        sum[0] = 0;
        for (int i = 0; i < len; i++) 
            sum[i + 1] = sum[i] + nums[i];
        
        for(int i=0; i<len; i++)
            dp[i] = sum[len]-sum[i];

        for (int k = 1; k < m; k++) 
            for (int i = 0;i < len - k;i ++) 
                dp[i] =  Integer.MAX_VALUE;
                for (int j = i + 1; j <= len - k;j++) 
                    dp[i] = Math.min(dp[i], Math.max(sum[j] - sum[i], dp[j]));
                
            

        
        return dp[0];

    

这道题目,采用递归,化为循环dp,优化空间三个步骤。

以上是关于[Leetcode] Split Array Largest Sum的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 842. Split Array into Fibonacci Sequence

[Leetcode] Split Array Largest Sum

[Leetcode] Split Array Largest Sum

**Leetcode 659. Split Array into Consecutive Subsequences

Leetcode 410. Split Array Largest Sum

leetcode 410. Split Array Largest Sum