Lintcode: Wood Cut
Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get from the n pieces of wood? Given L & k, return the maximum length of the small pieces. Have you met this question in a real interview? Yes
Example
For L=[232, 124, 456], k=7, return 114. Note
You couldn't cut wood into float length. Challenge
O(n log Len), where Len is the longest length of the wood.
注意是在[1,max(L)]里面找
public class Solution {
/**
*@param L: Given n pieces of wood with length L[i]
*@param k: An integer
*return: The maximum length of the small pieces.
*/
public int woodCut(int[] L, int k) {
// write your code here
if (L==null || L.length==0 || k<=0) return 0;
Arrays.sort(L);
int l=1, r=L[L.length-1];
while (l <= r) {
int m = l+(r-l)/2;
if (calc(L, m)>=k) l=m+1;
else r=m-1;
}
return r;
}
public int calc(int[] L, int len) {
int res = 0;
for (int i=0; i<L.length; i++) {
res += L[i]/len;
}
return res;
}
}
Lintcode: Wood Cut的更多相关文章
- 183.Wood Cut【hard】
183.Wood Cut[hard] Given n pieces of wood with length L[i] (integer array). Cut them into small piec ...
- Wood Cut
Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 二分查找总结及部分Lintcode题目分析 4
二分法不只能像之前的记录,可以找到index~第二种类型是找到二分答案.有以下几个例子,都是之前二分法的扩展,不再赘述,只记录下忽略的点,以后回顾多注意~ 1. wood cut class Solu ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
- 2017-3-7-lint183-wood-cut
2017-3-7-lint183-wood-cut 在河之洲 算法 lintcode problem lintcode183 wood cut solution 注意两点 注意边界条件 取的是最大值而 ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- Win8Metro(C#)数字图像处理--2.17图像木刻效果
原文:Win8Metro(C#)数字图像处理--2.17图像木刻效果 [函数名称] 图像木刻效果函数WoodCutProcess(WriteableBitmap src) [函数代码] ///& ...
- hdu.5203.Rikka with wood sticks(数学推导:一条长度为L的线段经分割后可以构成几种三角形)
Rikka with wood sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/O ...
随机推荐
- ActionResult,PartialViewResult,EmptyResult,ContentResult
HttpGet HttpPost HttpDelete HttpPut HeepHead HttpOptions HttpPatch属性都是动作方法选定器的一份子,比如若在action上套用H ...
- Centos下使用Heartbeat实现集群[转]
Linux 包括 CentOS 下高可用性(HA:High Availability)集群方案很多,而 Heartbeat 是比较常见和性价比比较高的一种。一、硬件及网络连接 群集一般需要2台以上服务 ...
- Finally 与 return
网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验,至少有两种情况下fina ...
- 使用dotTrace6.0进行内存分析
dotTrace6.0提供了内存分析功能,统计抓取的时间段内各个堆栈执行过程中使用的内存大小,按照堆栈执行情况树状排序:和它之前提供的时间统计类似,粗截了几个页面,希望对大家有所帮助. 下载安装Jet ...
- BAT(批处理)获得参数
原文转自:http://blog.csdn.net/luhouxiang/article/details/31733049 获取参数有2种, 一种为从命令行输入参数,使用两个%中间包含数字表示,数字从 ...
- angularJS自定义属性作为条件中转
<html> <head> <meta charset="utf-8"/> <title></title> </h ...
- 毕老师的Editplus
简介 EditPlus是一款由韩国 Sangil Kim (ES-Computing)出品的小巧但是功能强大的可处理文本.HTML和程序语言的Windows编辑器,你甚至可以通过设置用户工具将其作为C ...
- 实验一补充内容 Java开发环境的熟悉-刘蔚然
本次实验 PSP时间统计 步骤 耗时百分比 需求分析 5% 设计 10% 代码实现 67% 测试 15% 分析总结 3%
- zero3- JPA http://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html
1.很好的博客:http://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html 2. 最新的搬到github : http://holb ...
- echarts -01 入门
1.效果图 2. code <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...