leetcode410 Split Array Largest Sum

思路:

dp。

实现:

 1 class Solution
 2 {
 3 public:
 4     int splitArray(vector<int>& nums, int m)
 5     {
 6         int n = nums.size();
 7         vector<int> sum(n + 1, 0);
 8         for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + nums[i - 1];
 9         vector<vector<int>> dp(n + 1, vector<int>(m + 1, INT_MAX));
10         for (int i = 1; i <= n; i++) dp[i][1] = sum[i];
11         for (int i = 1; i <= n; i++)
12         {
13             for (int j = 2; j <= min(m, i); j++)
14             {
15                 for (int k = 1; k < i; k++)
16                 {
17                     int tmp = sum[i] - sum[k];
18                     dp[i][j] = min(dp[i][j], max(tmp, dp[k][j - 1]));
19                 }
20             }
21         }
22         return dp[n][m];
23     }
24 };

原文地址:https://www.cnblogs.com/wangyiming/p/9141403.html

时间: 2024-10-24 15:17:59

leetcode410 Split Array Largest Sum的相关文章

410. 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: Given m satisfies the following cons

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: Given m satisfies the following cons

[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:Given m satisfies the following const

410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

[抄题]: 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, as

动态规划——Split Array Largest Sum

题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among these m subarrays). Note:If n is the length of array, assume the following constraints are satisfied: 1 ≤ n ≤ 10001 ≤ m ≤ min(50, n) Examples: Input:num

Leetcode 410. Split Array Largest Sum

Problem: 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

Split Array Largest Sum LT410

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. Examples: Input: nums = [7,2,5,10,8] m = 2

LC410. Split Array Largest Sum

把一个数组分成m个连续子数组(不能有空数组),求所有分法中,子数组sum的最大值的最小值. 方法1:容易想到的是动态规划 dp[i][j] = min(max(dp[k-1][j-1], sum[k][i]) 1 <= k <= i, dp[i][j]表示用前i个数字,分成j组,最大和的最小值 time:$O(nnm)$ space:$O(nm)$ class Solution { public: typedef long long ll; ll INF = 1e15; int splitAr

Get the largest sum of contiguous subarray in an int array

When I finished reading this problem,I thought I could solve it by scan every single subarray in the array,and the time complexity is cubic.Every subarray could be the eventual one whose sum is the largest,so I did make a conclusion that the best tim