做了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的更多相关文章

  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. 410. Split Array Largest Sum 把数组划分为m组,怎样使最大和最小

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

  3. 【leetcode】410. Split Array Largest Sum

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

  4. 410 Split Array Largest Sum 分割数组的最大值

    给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小.注意:数组长度 n 满足以下条件:    1 ≤ n ≤ 1000 ...

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

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

  6. Split Array Largest Sum

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

  7. Leetcode: Split Array Largest Sum

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

  8. [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 ...

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

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

随机推荐

  1. Linux C++服务器程序设计范式

    <Unix网络编程>30章详细介绍了几种服务器设计范式.总结了其中的几种,记录一下: 多进程的做法: 1.每次创建一个新的请求,fork一个子进程,处理该连接的数据传输. 2.预先派生一定 ...

  2. Win32 GDI 非矩形区域剪裁,双缓冲技术

    传统的Win32通过GDI提供图形显示的功能,包括了基本的绘图功能,如画线.方块.椭圆等等,高级功能包括了多边形和Bezier的绘制.这样app就不用关心那些图形学的细节了,有点类似于UNIX上的X- ...

  3. 《Velocity java开发指南》中文版(下)转载

    文章出自:http://sakyone.iteye.com/blog/524292 8.Application Attributes Application Attributes (应用程序属性)是和 ...

  4. 关于Zen Coding:css,html缩写

    zen coding 是一个俄罗斯人写的编辑器(支持大部分现下流行的编辑器)插件,其安装也是非常简单,只要安装插件,然后在项目中拷贝js文件就可以.像Webstorm6.0.2中已经包含这样的插件.什 ...

  5. python调用java

    这么个标题多少有点蛋疼的感觉,两个都是互联网时代的语言,学习成本和执行效率也差不多,之所以会产生这种需求,多半是想在python中引用java的类,例如安卓和hadoop的生态圈,基本是java代码的 ...

  6. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  7. Matlab聚类分析[转]

    Matlab聚类分析[转] Matlab提供系列函数用于聚类分析,归纳起来具体方法有如下: 方法一:直接聚类,利用clusterdata函数对样本数据进行一次聚类,其缺点为可供用户选择的面较窄,不能更 ...

  8. 监控 DNS 流量,预防安全隐患五大招!

    尽管 IT 管理员尽心尽责地监控设备.主机和网络是否存在恶意活动的迹象,却往往出力不讨好.主机入侵检测和端点保护对很多公司来说可能是"必需"的安全措施,但如果要找出 RAT.roo ...

  9. Connect them

    zoj3204: 最小生成树,要求最小字典序的解. 用kruscal算法,先排序,输出的时候也要排序. /* zoj3204 解题思路: 赤裸裸的最小生成树.只是要求输出字典序最小的连接方案. 所以在 ...

  10. synchronize的心得

    记录一下synchronize(this).synchronize(A.class).private B  b= new B(); synchronize(b) .synchronize static ...