410. Split Array Largest Sum
做了Zenefits的OA,比面经里的简单多了。。害我担心好久
阴险的Baidu啊,完全没想到用二分,一开始感觉要用DP,类似于极小极大值的做法。
然后看了答案也写了他妈好久。
思路是再不看M的情况下,最终结果的取值范围是[最大的元素,所有元素之和],然后就是用二分在这个范围里找。
注意二分的取舍要配合取舍方程,我用的方程是,保证子集的和都小于等于二分测试的那个二分M,然后集合数小于要求的m。一旦子集数量超过m,说明我们测试的二分M太小了,弄大点,才能让每个子集多放点元素,从而使得总子集数不超过m.
所以返还失败的情况下 L = M + 1
返还成功并不代表就是解,它仅仅表示当前二分M可以使得分出的子集数不超过m,所以R=M作为候选,这样最后判断失败保留的R就是最后一次成功的判断。
反正我这里写了好久,一开始套用yes right, no left的二分判断,判断之后还要重新遍历一次,看看最后的值是否是正确的解,是的话返还R,不是的话返还R+1.
最后,数组里还混杂了Integer.MAX_VALUE,所以计算的时候得改成LONG才行。
主要是卡二分,80%的时间卡在二分上。。越做题越发现自己不会二分了。
ref: leetcode discuss board
(https://discuss.leetcode.com/topic/61315/java-easy-binary-search-solution-8ms)
public class Solution
{
public int splitArray(int[] nums, int m)
{
long max = 0; // largest sum
long maxVal = -1; // smallest sum
//Arrays.sort(nums);
for(int i = 0; i < nums.length;i++)
{
max += nums[i];
maxVal = Math.max(maxVal,nums[i]);
}
if(m == 1) return (int)max;
long res = search(nums,m,max,maxVal);
return (int)res;
}
public long search(int[] nums, int m, long max, long maxVal)
{
long L = maxVal;
long R = max;
while(L < R)
{
long M = L + (R - L)/2;
if(isOK(M,nums,m))
{
R = M;
}
else
{
L = M + 1;
}
}
return R;
}
public boolean isOK(long M, int[] nums, int m)
{
int num = 1;
long cur = 0;
for(int i = 0; i < nums.length;i++)
{
if(cur + nums[i] <= M)
{
cur += nums[i];
}
else
{
num++;
cur = nums[i];
if(num > m) return false;
}
}
return true;
}
/*
[1,2,3,4,5]
2
[1,2,3,4,5,5,6,7,8,9,10,13,15,16,17,18,23,25,46,58]
3
[1,2147483646]
1
*/
}
求Zenefits给个机会吧。。给你跪舔了。
410. Split Array Largest Sum的更多相关文章
- [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组,怎样使最大和最小
[抄题]: Given an array which consists of non-negative integers and an integer m, you can split the arr ...
- 【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 分割数组的最大值
给定一个非负整数数组和一个整数 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 ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- Leetcode: Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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
题意大概就是,给定一个包含非负整数的序列nums以及一个整数m,要求把序列nums分成m份,并且要让这m个子序列各自的和的最大值最小(minimize the largest sum among th ...
随机推荐
- asp.net mvc Ajax服务器跳转
1.过滤器权限验证 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowM ...
- Javascript中bind()方法的使用与实现
对于bind,我愣了下,这个方法常用在jquery中,用于为被选元素添加一个或多个事件处理程序. 查了下手册,发现bind的作用和apply,call类似都是改变函数的execute context, ...
- Java小例子(学习整理)-----学生管理系统-控制台版
1.功能介绍: 首先,这个小案例没有使用数据库,用集合的形式暂时保存数据,做测试! 功能: 增加学生信息 删除学生信息 修改学生信息 查询学生信息: 按照学号(精确查询) 按照姓名(模糊查询) 打 ...
- GDB源代码查找路径
在gdb程序的时候,有时候会发现源代码文件找不到,对于那些带调试信息的系统库或者第三方库,很多时候当你真正想gdb去追他源代码的时候你会发现gdb根本找不到这些源代码路径.这个时候有两种选择: [1] ...
- 关于移动div的具体实现(js)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Oracle中Long类型的使用与不可使用
ORA-01754 表只能包含一个LONG类型的列alter table 表名 add 字段名 long raw错误原因:数据表中同时建立了LONG RAW类型和LONG类型.------------ ...
- 使用PHP脚本来写Daemon程序
什么是Daemon进程 这又是一个有趣的概念,daemon在英语中是"精灵"的意思,就像我们经常在迪斯尼动画里见到的那些,有些会飞,有些不会,经常围着动画片的主人公转来转去,啰 ...
- jQuery 获取父元素、子元素、同级元素
详情:http://www.w3school.com.cn/jquery/jquery_traversing_ancestors.asp parent() 方法返回被选元素的直接父元素.(常用) ...
- 软件测试software testing summarize
软件测试(英语:software testing),描述一种用来促进鉴定软件的正确性.完整性.安全性和质量的过程.软件测试的经典定义是:在规定的条件下对程序进行操作,以发现程序错误,衡量软件质量,并对 ...
- 简单python2.7.3安装setuptools模块
下载setuptools https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg 安装 .6c11- ...