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 ...
随机推荐
- (转)java使用jsp servlet来防止csrf 攻击的实现方法
背景: 1.csrf知识 CSRF(Cross-site request forgery跨站请求伪造,也被称为“one click attack”或者session riding,通常缩写为CSRF或 ...
- java 简洁的分层实现
1.分页实现 分页实现是将所有查询结果保存在session对象或集合中,翻页时从session对象或集合中取出一页所需的数据显示.但是这种方法有两个最主要的缺点:一是用户看到的可能是过期数据:二是如果 ...
- 【转载】Leaflet 中文api
L.Map API各种类中的核心部分,用来在页面中创建地图并操纵地图. 使用 example // initialize the map on the "map" div with ...
- 模仿std::vector写线性表的几点感想
数据结构还是很早之前学的了,当时才刚学过C语言,实现得都很简单,最近决定重新打牢基础,于是重新开始实现书上的数据结构和算法. 模仿C++ Primer的StrVec以及std::vector,使用模板 ...
- STL 算法罗列 (转)
非修改性序列操作(12个) 循环 for_each() 对序列中的每个元素执行某操作 查找 find() 在序列中找出某个值的第一次出现的位置 find_if() 在序列中找出符合某谓词的第一个元素 ...
- linux性能监控——CPU、Memory、IO、Network
一.CPU 1.良好状态指标 CPU利用率:User Time <= 70%,System Time <= 35%,User Time + System Time <= 70%. 上 ...
- 关于使用PyExecJS+nodejs使用与js反混淆
来源:https://cuiqingcai.com/5024.html 梳理这篇博客的时候出问题,我默认的是jscript作为pyexcJs的引擎,问题很大,大部分的js都无法加载,各种包用不了,只能 ...
- 淘宝开源Web服务器Tengine基本安装步骤
Tengine 是由淘宝核心系统部基于Nginx开发的Web服务器,它在Nginx的基础上,针对大访问量 网站的需求,添加了很多功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,淘宝商 ...
- C++ 栈 (链表实现)
第一.基本概念 栈中的元素遵守“先进后出”的原则(LIFO,Last In First Out) 只能在栈顶进行插入和删除操作 压栈(或推入.进栈)即push,将数据放入栈顶并将栈顶指针加一 出栈(或 ...
- 在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法
在使用Ajax请求返回json数据的时候IE浏览器弹出下载保存对话框的解决方法 最近在做一个小东西,使用kindeditor上传图片的时候,自己写了一个上传的方法,按照协议规则通过ajax返回json ...