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 ...
随机推荐
- storm简介[ZZ]
场景 伴随着信息科技日新月异的发展,信息呈现出爆发式的膨胀,人们获取信息的途径也更加多样.更加便捷,同时对于信息的时效性要求也越来越高.举个搜索 场景中的例子,当一个卖家发布了一条宝贝信息时,他希望的 ...
- POJ 1159 Palindrome(LCS)
题目链接:http://poj.org/problem?id=1159 题目大意:给定一串字符,添加最少的字符,使之成为回文串. Sample Input 5 Ab3bd Sample Output ...
- Android中为APP创建快捷方式的原理(自己的理解)
我们首先来看Android中为APP创建快捷方式的原理: 从图上可以看出,Android大致分7步完成快捷方式的创建: 第一步:Android系统的launcher程序会调用它的pickShortcu ...
- BitMap(比特位)
所谓的Bit-map就是用一个bit位来标记某个元素对应的Value, 而Key即是该元素.由于采用了Bit为单位来存储数据,因此在存储空间方面,可以大大节省. 腾讯面试的时候,让写了一个BitMap ...
- html本地存储尝试
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Struts2文件配置 登陆页面
Struts 版本号 struts-2.3.16.3 web.xml 配置 <?xml version=”1.0″ encoding=”UTF-8″?> <web-app versi ...
- 中文版Chrome浏览器不支持12px以下字体的解决方案
中文版Chrome浏览器不支持12px以下字体的解决方案 Chrome 27之前的中文版桌面浏览器会默认设定页面的最小字号是12px,英文版则没有限制,主要是因为chrome认为汉字小于12px就会增 ...
- PHP 面向对对象基础(接口,类)
介绍PHP面向对象的基础知识 1. 接口的定义interface ,类定义class,类支持abstract和final修饰符,abstract修饰为抽象类,抽象类 不支持直接实例化,final修饰的 ...
- AngularJS初体验
最近突然发现,Coding.net真是一个神奇的网站.这各网站90%的请求都是通过ajax完成的.可以发现,不管你点任何链接,网页都不会刷新,点击浏览器的返回或前进按钮也是这样,打开chrome的开发 ...
- Python抓取双色球数据
数据来源网站http://baidu.lecai.com/lottery/draw/list/50?d=2013-01-01 HTML解析器http://pythonhosted.org/pyquer ...