410 Split Array Largest Sum the maximum value of the split arrays

Source: Internet
Author: User
Tags array length

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.