Given a non-negative integer array and an integer m, you need to divide the array into m non-empty contiguous sub-arrays. An algorithm is designed to minimize the maximum value of each of these m arrays.
Attention:
The array length n satisfies the following conditions:
1≤n≤1000
1≤m≤min (N)
Example:
Input:
Nums = [7,2,5,10,8]
m = 2
Output:
18
Explain:
There are four ways to divide nums into 2 sub-arrays.
The best way is to divide it into [7,2,5] and [10,8],
Because at this point the maximum value for each of these two sub-arrays is 18, the smallest in all cases.
See: https://leetcode.com/problems/split-array-largest-sum/description/
C++:
Class Solution {Public:int Splitarray (vector<int>& nums, int m) {Long Long left = 0, right = 0; for (int i = 0; i < nums.size (); ++i) {left = max ((int.) left, Nums[i]); Right + = Nums[i]; (Left < right) {Long long mid = left + (right-left)/2; if (Can_split (Nums, M, mid)) {right = Mid; } else {left = mid + 1; }} return left; } bool Can_split (vector<int>& nums, int m, int sum) {int cnt = 1, cursum = 0; for (int i = 0; i < nums.size (); ++i) {cursum + = Nums[i]; if (Cursum > Sum) {cursum = nums[i]; ++cnt; if (cnt > m) {return false; }}} return true; }};
Reference: https://www.cnblogs.com/grandyang/p/5933787.html
410 Split Array Largest Sum the maximum value of the split arrays