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.
You couldn't cut wood into float length.
For L=[232, 124, 456]
, k=7
, return 114
.
Analysis:
From 1 to Max(L), use binary search to find the maximum length;
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) {
if (L == null || L.length == ) return ;
int startLength = ;
int endLength = maxLength(L); while (startLength <= endLength) {
int mid = startLength + (endLength - startLength) / ;
if (count(L, mid) >= k) {
startLength = mid + ;
} else {
endLength = mid - ;
}
}
return startLength - ; // using startLength will make count(L, startLength) < k
} public int count(int[] L, int length) {
int total = ;
for (int i = ; i < L.length; i++) {
total += L[i] / length;
}
return total;
} public int maxLength(int[] L) {
int max = L[];
for (int i = ; i < L.length; i++) {
max = Math.max(L[i], max);
}
return max;
}
}
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 ...
- Lintcode: 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 ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- 2017-3-7-lint183-wood-cut
2017-3-7-lint183-wood-cut 在河之洲 算法 lintcode problem lintcode183 wood cut solution 注意两点 注意边界条件 取的是最大值而 ...
- Win8Metro(C#)数字图像处理--2.17图像木刻效果
原文:Win8Metro(C#)数字图像处理--2.17图像木刻效果 [函数名称] 图像木刻效果函数WoodCutProcess(WriteableBitmap src) [函数代码] ///& ...
- 二分查找总结及部分Lintcode题目分析 4
二分法不只能像之前的记录,可以找到index~第二种类型是找到二分答案.有以下几个例子,都是之前二分法的扩展,不再赘述,只记录下忽略的点,以后回顾多注意~ 1. wood cut class Solu ...
- 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 ...
随机推荐
- Redis 基础:Redis 数据类型
Redis 数据类型 Redis支持五种数据类型:string(字符串).hash(哈希).list(列表).set(集合)及zset(sorted set:有序集合). String(字符串) st ...
- python配合Fiddler获取windows app登录时生成cookie实例
工具Fiddler/python3 打开Fiddler,清空一下Fidder里面的请求记录 打开app,进行登录,注意Fiddler里的请求变化 在弹出app登录的时候Fiddler里已经有了四个请求 ...
- oracle 月份中日的值必须介于 1 和当月最后一日之间
解决方法: 1.用时间字段去关联字符串字段导致此错误.. 如果1.解决不了就看 2.把date'2017-01-01' 换成 to_date('2017-01-01','yyyy-mm-dd')
- BZOJ 3165: [Heoi2013]Segment
3165: [Heoi2013]Segment Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 465 Solved: 187[Submit][Sta ...
- BZOJ 2938: [Poi2000]病毒
2938: [Poi2000]病毒 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 693 Solved: 360[Submit][Status][Di ...
- 一次绕过防火墙获取RCE以及提权到root权限的渗透过程
本文是关于Apache struts2 CVE-2013-2251是由于导致执行远程命令的影响而被高度利用的漏洞.简而言之, 通过操纵以“action:”/”redirect:”/”redirectA ...
- bzoj3463【COCI2012】 Inspector
题目描述 在一个小国家中,一个新的小镇终于建成了!如往常一样,Mirko获得了“首席税务巡查员”的职位.他的任务是保证正确地计算各公司的收入情况.一共有N家办公室坐落在主干道上,从左到右被编号为1~N ...
- Python之旅:入门
一 编程与编程语言 python是一门编程语言,作为学习python的开始,需要事先搞明白:编程的目的是什么?什么是编程语言?什么是编程? 编程的目的: #计算机的发明,是为了用机器取代/解放人力,而 ...
- opncv视频资料
链接: http://pan.baidu.com/s/1i37nXSL 密码: 3xnd这一套opncv资料包括视频和pdf资料
- sidecar学习
1.SideCar的出现 微服务的结构是细粒度的,由多个服务构成,支持不同的服务用不同的语言来编写,比如a服务用python,b服务用java,C服务用php等,我们称为异构语言,那么在利用zuul来 ...