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, assume the following constraints are satisfied:
- 1 ≤ n ≤ 1000
- 1 ≤ m ≤ min(50, n)
Examples:
Input:
nums = [7,2,5,10,8]
m = 2 Output:
18 Explanation:
There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8],
where the largest sum among the two subarrays is only 18.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道干嘛用二分法:二分法可以通过mid的移动 找一个位置
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
想不到:最大的数肯定要分隔开,就看能往左划几个数和它一组
[7,2,5,10,8,105550]
3
返回105550
分组不超过m就往左扩展 否则往右
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
l r都必须定义成long型,在返回的时候切换回来
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
多试试几个case 自然就明白了
数组中找一个位置,就可以用二分查找
[复杂度]:Time complexity: O(lgn) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:迭代
[关键模板化代码]:
while (l <= r) {
long mid = (l + r) / 2;
if (noLongerThanM(mid, nums, m)) r = mid - 1;
else l = mid + 1;
}
return (int)l;
}
[其他解法]:
dp麻烦
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
class Solution {
public int splitArray(int[] nums, int m) {
//ini: sum, l, r
int max = nums[0]; long sum = 0;
for (int num : nums) {
sum += num;
max = Math.max(max, num);
}
//cc
if (nums.length == 1) return (int)sum;
long l = max, r = sum;
// b - s
while (l <= r) {
long mid = (l + r) / 2;
if (noLongerThanM(mid, nums, m)) r = mid - 1;
else l = mid + 1;
}
return (int)l;
}
public boolean noLongerThanM(long value, int[] nums, int m) {
int count = 1, sum = 0;
for (int num : nums) {
sum += num;
if (sum > value) {
sum = num;
count++;
if (count > m) return false;
}
}
return true;
}
}
410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小的更多相关文章
- [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 ...
- 410 Split Array Largest Sum 分割数组的最大值
给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小.注意:数组长度 n 满足以下条件: 1 ≤ n ≤ 1000 ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 【leetcode】410. Split Array Largest Sum
题目如下: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 410. Split Array Largest Sum
做了Zenefits的OA,比面经里的简单多了..害我担心好久 阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法. 然后看了答案也写了他妈好久. 思路是再不看M的情况下 ...
- [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 ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 动态规划——Split Array Largest Sum
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- 1G1核1M选择 Centos 32位 还是 Centos 64位?
前几天有个疑惑,现有一台云主机是 1G1核1M使用 Centos 64位会不有点浪费. 还专门发信息询问老大 Karson,老大说现 FastAdmin 都是三个1,也是 64 位的. 看 FastA ...
- 在 FastAdmin 中启动 ThinkPHP 5 的请求缓存分析
在 FastAdmin 中启动 ThinkPHP 5 的请求缓存分析 缓存的基础配置 ThinkPHP 5 中有一个请求缓存:1 'request_cache' => true, 'reques ...
- 基于Video4Linux的视频采集模块开发(转)
Linux系统中,摄像头驱动程序安装好后,为了进行视频采集必须加入Video4Linux模块,从而可以通过Video4Linux模块提供的编程接口(API)从摄像头设备中获取图像帧.下面具体研究基于V ...
- 记一次socket_create()函数耗时异常记录
背景: 下午开发时突然整个页面耗时增加,空接口每次都需要2-3秒的耗时,一开始以为连开发环境数据库出现问题,最后断开数据库跑,发现还是很慢 最终逐步调试此页面耗时,定位到了socket_create( ...
- OPC接口相关资料地址
OPC官方网址:https://opcfoundation.org/ OPC中国官网: http://www.chinaopc.org/ ------------------------------- ...
- 基于标准库的string类实现简单的字符串替换
感觉基本功还是不扎实,虽然能做些程序但是现在看来我还是个初学者(primer),试着完成习题结果还得修修改改. 废话不多说,实现功能很简单,<C++ Primer>9.5.2节习题. // ...
- Java报错 -- The public type c must be defined in its own file
出现The public type c must be defined in its own file这个问题,是由于定义的JAVA类同文件名不一致 你的文件里很可能有两个 public 的类,而Ja ...
- Vim编辑器基本操作学习(二)
操作符+位移 x命令可以删除一个字符,4x可以删除4个字符. dw可以删除一个word,w事实上是向后移动一个word的命令:dw可以接上一个任意一个位移命令,它将删除从当前光标开始到位移终点处的文本 ...
- WindowsPhone自定义控件详解(一) - 控件类库分析
转自:http://blog.csdn.net/mr_raptor/article/details/7251942 为了让你的应用程序更有个性,我们通常会在WP7开发过程中会自定义自己风格的控件,自定 ...
- FBVector
folly/FBVector.h Simply replacing std::vector with folly::fbvector (after having included the folly/ ...