给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组。设计一个算法使得这 m 个子数组各自和的最大值最小。
注意:
数组长度 n 满足以下条件:
    1 ≤ n ≤ 1000
    1 ≤ m ≤ min(50, n)
示例:
输入:
nums = [7,2,5,10,8]
m = 2
输出:
18
解释:
一共有四种方法将nums分割为2个子数组。
其中最好的方式是将其分为[7,2,5] 和 [10,8],
因为此时这两个子数组各自的和的最大值为18,在所有情况中最小。
详见: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];
}
while (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;
}
};

参考:https://www.cnblogs.com/grandyang/p/5933787.html

410 Split Array Largest Sum 分割数组的最大值的更多相关文章

  1. [LeetCode] 410. Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  2. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

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

    [抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  4. 【leetcode】410. Split Array Largest Sum

    题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...

  5. 410. Split Array Largest Sum

    做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...

  6. [Swift]LeetCode410. 分割数组的最大值 | Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  7. Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  8. 动态规划——Split Array Largest Sum

    题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...

  9. Leetcode: Split Array Largest Sum

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

随机推荐

  1. ****Call to a member function item() on a non-object

    A PHP Error was encountered Severity: Error Message: Call to a member function item() on a non-objec ...

  2. JDBC驱动类型

    一下内容引用自http://wiki.jikexueyuan.com/project/jdbc/drive-types.html: 一.什么是JDBC驱动程序? JDBC驱动实现了JDBC API中定 ...

  3. Servlet自动刷新页面

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/auto-refresh.html: 假设一个Web页面,显示了现场比赛得分或股票市场状况或货币兑 ...

  4. top命令查看线程信息和jstack使用介绍

    top -Hp pid可以查看某个进程的线程信息 -H 显示线程信息,-p指定pid jstack 线程ID 可以查看某个线程的堆栈情况,特别对于hung挂死的线程,可以使用选项-F强制打印dump信 ...

  5. 剑指Offer - 开始没做出来 —— 验证后序序列是否正确

    https://www.nowcoder.net/practice/a861533d45854474ac791d90e447bafd?tpId=13&tqId=11176&tPage= ...

  6. Django学习系列之Cookie、Session

    Cookie和Session介绍 cookie 保存在客户端 session 保存在服务端 session依赖于cookie,比如服务端想往客户端写东西的时候就把cookie写到客户端浏览器 djan ...

  7. datagrid行操作

    //获取第一个被选中的行 var row=$('#dg').datagrid('getSelected'); //获取行对应的索引值 var index=$('#dg').datagrid('getR ...

  8. 1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise

    题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chine ...

  9. 2016/2/19 position: fixed absolute relative z-index float 半透明效果

    一.position:fixed 锁定位置(相对于浏览器的位置),例如有些网站的右下角的弹出窗口.      显示效果  无论滚动条怎么移动  都固定在显示页面的一个位置不动 二.position:a ...

  10. LeetCode——Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...