动态规划-划分数组的最大和 Split Array Largest Sum

2019-10-14 22:13:18

问题描述

动态规划-划分数组的最大和 Split Array Largest Sum_第1张图片

问题求解

经典的动态规划问题。

    public int splitArray(int[] nums, int m) {
        int n = nums.length;
        long[][] dp = new long[m + 1][n];
        long[] presum = new long[n];
        long curSum = 0;
        for (int i = 0; i < n; i++) {
            curSum += nums[i];
            presum[i] = curSum;
            dp[1][i] = curSum;
        }
        for (int i = 2; i <= m; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j] = curSum;
                for (int k = 0; k < j; k++) {
                    dp[i][j] = Math.min(dp[i][j], Math.max(dp[i - 1][k], presum[j] - presum[k]));
                }
            }
        }
        return (int)dp[m][n - 1];
    }

  

 

你可能感兴趣的:(动态规划-划分数组的最大和 Split Array Largest Sum)