Lintcode: 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. Have you met this question in a real interview? Yes
Example
For L=[232, 124, 456], k=7, return 114. Note
You couldn't cut wood into float length. Challenge
O(n log Len), where Len is the longest length of the wood.
注意是在[1,max(L)]里面找
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) {
// write your code here
if (L==null || L.length==0 || k<=0) return 0;
Arrays.sort(L);
int l=1, r=L[L.length-1];
while (l <= r) {
int m = l+(r-l)/2;
if (calc(L, m)>=k) l=m+1;
else r=m-1;
}
return r;
}
public int calc(int[] L, int len) {
int res = 0;
for (int i=0; i<L.length; i++) {
res += L[i]/len;
}
return res;
}
}
Lintcode: 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 ...
- 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 ...
- 二分查找总结及部分Lintcode题目分析 4
二分法不只能像之前的记录,可以找到index~第二种类型是找到二分答案.有以下几个例子,都是之前二分法的扩展,不再赘述,只记录下忽略的点,以后回顾多注意~ 1. wood cut class Solu ...
- 二分难题 && deque
141. Sqrt(x) https://www.lintcode.com/problem/sqrtx/description?_from=ladder&&fromId=4 publi ...
- 2017-3-7-lint183-wood-cut
2017-3-7-lint183-wood-cut 在河之洲 算法 lintcode problem lintcode183 wood cut solution 注意两点 注意边界条件 取的是最大值而 ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- Win8Metro(C#)数字图像处理--2.17图像木刻效果
原文:Win8Metro(C#)数字图像处理--2.17图像木刻效果 [函数名称] 图像木刻效果函数WoodCutProcess(WriteableBitmap src) [函数代码] ///& ...
- 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 ...
随机推荐
- 初学Java语法(笔记)
2015-12-30
- 使用Xpath对XML进行模糊查询
如果要对XML文件进行模糊查找的话是一个比较麻烦的事情,Xpath表达式中没有像文件系统中的“*”或"?" 或者有像SQL表达式中的"%",这样的模糊查找的通配 ...
- miniproject black jack--Fail
第一部分 下载这个小项目的程序模板并回顾card类的定义.这个类已经执行了所以你的任务是自己熟悉下代码.开始,通过粘贴card类定义到程序模板中并验证我们的代码如预期那样工作. 实现“__init__ ...
- 模板-高精度BigInteger
#include <bits/stdc++.h> using namespace std; struct BigInteger { static const int BASE = 1000 ...
- OpenMP for Fortran
OpenMP for Fortran OpenMP Directive Syntax of OpenMP compiler directive for Fortran: !$OMP Directive ...
- 【Demo】微信上墙
先看看微信墙效果图 使用简单说明 关于微信公众号 回复 "上墙",点击授权文章进行授权 回复"#上墙内容" 即可发表上墙消息了 查看微信墙列表,点击这里 原文地 ...
- myeclipse调式与属性显示
最近做项目的时候发现一个奇怪的东西,当我用myeclipse调式时,调式窗口显示实体user所关联的role的对象属性是空的,但是,从syst打印出来是有的,最近感到很奇怪,后来发现这只是调式的一种显 ...
- LightOj 1024 - Eid (求n个数的最小公约数+高精度)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1024 题意:给你n(2<=n<=1000)个数, 然后求n个数的最小公倍数 ...
- Surround the Trees---hdu1392(凸包GraHam模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:有n棵树,每棵树有一个坐标,想用一些绳子把这些树包含起来,求需要绳子的长度: 就是求凸包的 ...
- SqlServer基础:IsNull
SELECT @temp = ISNULL(point, 0) FROM dbo.User where Nid=6 如果User表中的point字段为null的话,则对@temp赋值0