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 ...
随机推荐
- python自动化之图像
''' RGBA值:指定颜色中的红.绿.蓝和alpha(透明度)的值 RGBA 名称 (255,255,255,2 ...
- 【转载】JSP 获取真实IP地址的代码
JSP 获取真实IP地址的代码 在JSP里,获取客户端的IP地址的方法是:request.getRemoteAddr(),这种方法在大部分情况下都是有效的. 但是在通过了 Apache,Squid ...
- 【刷题】BZOJ 3524 [Poi2014]Couriers
Description 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0 ...
- java多线程 -- Condition 控制线程通信
Api文档如此定义: Condition 将 Object 监视器方法(wait.notify 和 notifyAll)分解成截然不同的对象,以便通过将这些对象与任意 Lock 实现组合使用,为每个对 ...
- 解题:CEOI 2017 Mousetrap
题外话: 这是制杖yd的交流题目 题面 首先把捕鼠夹所在的点提出来当根,然后这变成了一棵有根树,我们先来看耗子移动的影响 可以发现耗子往下走就回不来了,而且最后还会被困在一个叶子上,那么这个时候我们把 ...
- HDU 5306 线段树
吉司机课件题. 区间min,区间最值,区间和. 如果用最大值和次大值能更新区间和那就更新打标记,否则暴力dfs. #include<iostream> #include<cstdio ...
- bzoj4427【Nwerc2015】Cleaning Pipes清理管道
题目描述 Linköping有一个相当复杂的水资源运输系统.在Linköping周围的出水点有一些水井.这些水通过管道输送到其它地点.每条管道是从某一个水井到城市的某个位置的直线管道. 所有管道在地下 ...
- Python【pymysql】模块
import pymysql# 1.连上数据库 账号.密码 ip 端口号 数据库#2.建立游标#3.执行sql#4 .获取结果# 5.关闭游标#6.连接关闭coon = pymysql.connect ...
- 51NOD 1105 第K大的数
数组A和数组B,里面都有n个整数. 数组C共有n^2个整数,分别是: A[0] * B[0],A[0] * B[1] ...... A[0] * B[n-1] A[1] * B[0],A[1] * B ...
- Hadoop生态圈-Hive快速入门篇之Hive环境搭建
Hadoop生态圈-Hive快速入门篇之Hive环境搭建 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据仓库(理论性知识大多摘自百度百科) 1>.什么是数据仓库 数据 ...