Wood Cut
2016-12-13 10:44
95 查看
Given n pieces of wood with length
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.
Notice
You couldn't cut wood into float 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.
Notice
You couldn't cut wood into float 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 == 0 || k == 0) { return 0; } int len = L[0]; for(int l: L) { len = Math.max(len, l); } int start = 0; int end = len; while(start + 1 < end) { int mid = start + (end - start) / 2; int count = cut(L, mid); if(count < k) { end = mid; } else { start = mid; } } if(cut(L, end) >= k) { return end; } else { return start; } } private int cut(int[] L, int len) { int count = 0; for(int l: L) { count += l / len; } return count; } }
相关文章推荐
- Search Engine XSS Worm
- 通过使用正确的search arguments来提高SQL Server数据库的性能
- 简介JavaScript中search()方法的使用
- JavaScript禁止复制与粘贴的实现代码
- js闭包实现按秒计数
- 向大家推荐一个收集整理正则表达式的网站
- Js&Vbs正则表达式替换重复的字符
- 限制文本框中只能输入实数或整数,其它字符无效,有劳大家了!
- 关于IE的RegExp.exec的问题
- 最严谨的校验email地址的正则表达式及各种语言对应版
- Google Map Api和GOOGLE Search Api整合实现代码
- javascript中的window.location.search方法简介
- 使用javaScript动态加载Js文件和Css文件
- 详解centos7上elastic search安装及填坑记
- 教你使用javascript简单写一个页面模板引擎
- Microsoft Search 服务无法启动 解决办法.
- MYSQL的binary解决mysql数据大小写敏感问题的方法
- JavaScript操作 url 中 search 部分方法函数
- 正则表达式中特殊符号及正则表达式的几种方法总结(replace,test,search)
- PHP 在数组中搜索给定的简单实例 array_search 函数