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 ...
随机推荐
- Digits of Factorial LightOJ - 1045(数学题?)
原文地址: https://blog.csdn.net/fenghoumilin/article/details/52293910 题意:求 n 的阶乘在 base 进制下的位数,这里有一个简单的方法 ...
- git gitosis 添加项目
example: 1, user@my-test:~/perl_src$ git inituser@my-test:~/perl_src$ git add .user@my-test:~/perl_s ...
- 【LaTex】随便学学,
教程 http://blog.csdn.net/u014803202/article/details/50410748 一个数学公式编辑器 http://latex.91maths.com/
- view的阴影效果shadowColor
btn.layer.shadowColor = UIColor.blackColor().CGColor btn.layer.shadowOffset = CGSizeMake(5, 5) btn.l ...
- 关于Swift中的泛函数find的问题
对于一个数组Array,我们往往需要判断其是否包含某个子项,又或者要查找某个子项是否在这个数组中. 假设有这样一个包含坐标的数组 var pointArray:[CGPoint] = [CGPoint ...
- 模板:快速傅里叶变换(FFT)
参考:http://blog.csdn.net/f_zyj/article/details/76037583 如果公式炸了请去我的csdn博客:http://blog.csdn.net/luyouqi ...
- HDU.1847 Good Luck in CET-4 Everybody! ( 博弈论 SG分析)
HDU.1847 Good Luck in CET-4 Everybody! ( 博弈论 SG分析) 题意分析 简单的SG分析 题意分析 简单的nim 博弈 博弈论快速入门 代码总览 //#inclu ...
- VIM 模板
Vim实现自动加载模版功能可以有很多的方法,比如利用插件和AutoCmd等.根据文件名自动加载模板的功能利用网上某大牛自己写的插件实现,我针对Java代码进行简单地修改,以实现模板中的Java主类类名 ...
- D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)
http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 #include <cstdio ...
- Spyder使用IPython Console弹出绘图窗口的设置方法
http://www.datastudy.cc/article/3cfc3aff3b2c5948b938456e00376276 在使用Spyder的过程中,因为它的绘图,默认是绘图在IPython窗 ...