Binary Search---Wood Cut
2016-04-03 18:36
465 查看
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.
For L=[232, 124, 456], k=7, return 114.
For L=[232, 124, 456], k=7, return 114.
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) { int max = 0; for (int i = 0; i < L.length; i++) { max = Math.max(max, L[i]); } // find the largest length that can cut more than k pieces of wood. int start = 1, end = max; while (start + 1 < end) { int mid = start + (end - start) / 2; if (count(L, mid) >= k) { start = mid; } else { end = mid; } } if (count(L, end) >= k) { return end; } if (count(L, start) >= k) { return start; } return 0; } private int count(int[] L, int length) { int sum = 0; for (int i = 0; i < L.length; i++) { sum += L[i] / length; } return sum; } }
相关文章推荐
- bash脚本总结
- 【C++】Geekband - 专题一:new和delete的整理
- Sublime Es6教程1-环境搭建
- 使用butterknife zelezny插件时必须保证已导入butterKnife的库工程。
- MVVM下DataGrid的简单应用
- python截取郑州大学贴吧网页
- dos批处理命令详解
- Win10下的Telnet
- javascript设计模式 单体模式
- leetcode——101——Symmetric Tree
- translation from thephilosophy writing of gottlob frege
- 设置 Linux 文件和文件夹权限的方法
- MySQL MERGE引擎简介
- 各种颜色RGB值【附c#中获取颜色的argb值】
- 在s5pv210开发板上移植官方2101310版本的uboot
- BZOJ2671: Calc
- Scala语言基础
- grub应用
- linux帐号防暴力破解脚本
- 计算机图形学三角形基元填充算法即三角形光栅化重心双线性插值算法