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.

Example

For L=[232, 124, 456]k=7, return 114.

Analysis:

From 1 to Max(L), use binary search to find the maximum length;

  1. public class Solution {
  2. /**
  3. *@param L: Given n pieces of wood with length L[i]
  4. *@param k: An integer
  5. *return: The maximum length of the small pieces.
  6. */
  7. public int woodCut(int[] L, int k) {
  8. if (L == null || L.length == ) return ;
  9. int startLength = ;
  10. int endLength = maxLength(L);
  11.  
  12. while (startLength <= endLength) {
  13. int mid = startLength + (endLength - startLength) / ;
  14. if (count(L, mid) >= k) {
  15. startLength = mid + ;
  16. } else {
  17. endLength = mid - ;
  18. }
  19. }
  20. return startLength - ; // using startLength will make count(L, startLength) < k
  21. }
  22.  
  23. public int count(int[] L, int length) {
  24. int total = ;
  25. for (int i = ; i < L.length; i++) {
  26. total += L[i] / length;
  27. }
  28. return total;
  29. }
  30.  
  31. public int maxLength(int[] L) {
  32. int max = L[];
  33. for (int i = ; i < L.length; i++) {
  34. max = Math.max(L[i], max);
  35. }
  36. return max;
  37. }
  38. }

Wood Cut的更多相关文章

  1. 183.Wood Cut【hard】

    183.Wood Cut[hard] Given n pieces of wood with length L[i] (integer array). Cut them into small piec ...

  2. Lintcode: Wood Cut

    Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. 二分难题 && deque

    141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...

  5. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  6. 2017-3-7-lint183-wood-cut

    2017-3-7-lint183-wood-cut 在河之洲 算法 lintcode problem lintcode183 wood cut solution 注意两点 注意边界条件 取的是最大值而 ...

  7. Win8Metro(C#)数字图像处理--2.17图像木刻效果

    原文:Win8Metro(C#)数字图像处理--2.17图像木刻效果  [函数名称] 图像木刻效果函数WoodCutProcess(WriteableBitmap src) [函数代码] ///& ...

  8. 二分查找总结及部分Lintcode题目分析 4

    二分法不只能像之前的记录,可以找到index~第二种类型是找到二分答案.有以下几个例子,都是之前二分法的扩展,不再赘述,只记录下忽略的点,以后回顾多注意~ 1. wood cut class Solu ...

  9. 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 ...

随机推荐

  1. Digits of Factorial LightOJ - 1045(数学题?)

    原文地址: https://blog.csdn.net/fenghoumilin/article/details/52293910 题意:求 n 的阶乘在 base 进制下的位数,这里有一个简单的方法 ...

  2. git gitosis 添加项目

    example: 1, user@my-test:~/perl_src$ git inituser@my-test:~/perl_src$ git add .user@my-test:~/perl_s ...

  3. 【LaTex】随便学学,

    教程 http://blog.csdn.net/u014803202/article/details/50410748 一个数学公式编辑器 http://latex.91maths.com/

  4. view的阴影效果shadowColor

    btn.layer.shadowColor = UIColor.blackColor().CGColor btn.layer.shadowOffset = CGSizeMake(5, 5) btn.l ...

  5. 关于Swift中的泛函数find的问题

    对于一个数组Array,我们往往需要判断其是否包含某个子项,又或者要查找某个子项是否在这个数组中. 假设有这样一个包含坐标的数组 var pointArray:[CGPoint] = [CGPoint ...

  6. 模板:快速傅里叶变换(FFT)

    参考:http://blog.csdn.net/f_zyj/article/details/76037583 如果公式炸了请去我的csdn博客:http://blog.csdn.net/luyouqi ...

  7. HDU.1847 Good Luck in CET-4 Everybody! ( 博弈论 SG分析)

    HDU.1847 Good Luck in CET-4 Everybody! ( 博弈论 SG分析) 题意分析 简单的SG分析 题意分析 简单的nim 博弈 博弈论快速入门 代码总览 //#inclu ...

  8. VIM 模板

    Vim实现自动加载模版功能可以有很多的方法,比如利用插件和AutoCmd等.根据文件名自动加载模板的功能利用网上某大牛自己写的插件实现,我针对Java代码进行简单地修改,以实现模板中的Java主类类名 ...

  9. D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 #include <cstdio ...

  10. Spyder使用IPython Console弹出绘图窗口的设置方法

    http://www.datastudy.cc/article/3cfc3aff3b2c5948b938456e00376276 在使用Spyder的过程中,因为它的绘图,默认是绘图在IPython窗 ...