Wood Cut
来源:互联网 发布:radon内核优化项目 编辑:程序博客网 时间:2024/05/17 06:55
有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k。当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度。
注意事项:
木头长度的单位是厘米。原木的长度都是正整数,我们要求切割得到的小段木头的长度也要求是整数。无法切出要求至少 k 段的,则返回 0 即可。
样例
有3根木头[232, 124, 456], k=7, 最大长度为114.
分析:常见的二分法解决的为index问题,此题可以通过查找k的位置来转化为二分问题。将寻常二分nums[mid] == target,替换为检查func() == k来实现。
public int woodCut(int[] L, int k) { if (L == null || L.length == 0) { return 0; } int max = 0; //the longest length for (int i = 0; i < L.length; i++) { max = Math.max(max,L[i]); } int start = 1; int end = max; while (start + 1 < end) { int mid = start + (end - start) / 2; if (count(L,mid) == k){ //binary search start = mid; } else if (count(L,mid) < k) { end = mid; } else { start = mid; } } if (count(L,end) >= k) { return end; } if (count(L,start) >= k) { return start; } return 0; } //count the piece of wood when it is the length private int count (int[] L, int length) { int num = 0; for (int i = 0; i < L.length; i++) { num += L[i] / length; } return num; }
0 0
- wood cut
- wood-cut
- Wood Cut
- Wood Cut
- lintcode:Wood Cut
- Binary Search---Wood Cut
- #183 Wood Cut
- [LintCode]Wood Cut
- lintcode之Wood-Cut问题
- LintCode Wood Cut java solution
- 二分查找——Wood Cut
- 02分治算法-03wood-cut
- wood
- 183.Wood Cut-木材加工(中等题)
- Slice Wood
- cut
- cut
- cut
- javascript基础:Math对象
- LeetCode 179. Largest Number
- redis.conf 配置说明
- NetError IOException:connection-refused--Dbeaver连sqlServer报错
- 设备坐标和逻辑坐标
- Wood Cut
- Linux expect安装
- 因为只考了280所以心情不爽写出的总结反思
- 1036. Boys vs Girls (25)
- 数组
- 自定义接口内部类的两个具体应用(跨类传值)
- 记录cell自适应,以及cell内容动态添加问题.
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍
- Java:学习路线