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 ...
随机推荐
- BZOJ 3173 最长上升子序列(树状数组+二分+线段树)
给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上升子序列长度是多少? 由于序列是顺序插入的,所以当前插入的数字对之 ...
- 概率主题模型简介 Introduction to Probabilistic Topic Models
此文为David M. Blei所写的<Introduction to Probabilistic Topic Models>的译文,供大家参考. 摘要:概率主题模型是一系列旨在发现隐藏在 ...
- CF992C Nastya and a Wardrobe
我是题面 题意很清晰,这种题,我们当然还是有两种方法来做啦 方法一:找规律 读完题我们来看样例,通过样例一已我们大概可以看出,答案或许是\(n*2^{k+1}\) 肯定不能这么简单对吧,那就来看样例二 ...
- eclispe 出现超内存错误
刚开始以为只要修改tomcat的最大最小内存就可以,结果还是报错,后来才懂需要在eclipse.ini文件中修改 -Xms256m-Xmx512m的值改大些,增加虚拟机运行的内存空间 刚开始最小值只有 ...
- 查看临时表空间占用最多的用户与SQL
select sess.username, sql.sql_text, sort1.blocks from v$session sess, v$sqlarea sql, v$sort_usage ...
- MT【130】Heilbronn问题
(清华THUSSAT,多选题) 平面上 4 个不同点 \(P_1,P_2,P_3,P_4\),在每两个点之间连接线段得到 6 条线段. 记 \[L=\max_{1\leq i<j\leq 4}| ...
- poj2761 feed the dog
题目链接:http://poj.org/problem?id=2761 Description Wind loves pretty dogs very much, and she has n pet ...
- AS可视化布局中文乱码
求助android studio 的可视化布局中文乱码-CSDN论坛-CSDN.NET-中国最大的IT技术社区http://bbs.csdn.net/topics/391887442 Android ...
- 图像处理之FPN校正
1 FPN噪声介绍 FPN噪声(Fixed Pattern Noise)简称固定模式噪声,根据FPN噪声形成机制,分为行FPN和列FPN.行FPN: 在基于模拟域累加实现的TDI-CMOS图像传感器中 ...
- centos 安装MATLAB :设置回环设备失败: 没有那个文件或目录
基本参数:centos 7 x86_64,linux 系统, 安装matlab, 已经下载R2016b_glnxa64.iso 但挂载的时候遇到问题: [root@lf mnt]# mount -o ...