LintCode Wood Cut
2015-09-13 12:23
211 查看
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.
这个好像在挑战编程里看到过类似的,用二分搜索,用upper_bound的思想,注意除零的情况。
class Solution { public: /** *@param L: Given n pieces of wood with length L[i] *@param k: An integer *return: The maximum length of the small pieces. */ int woodCut(vector<int> L, int k) { // write your code here long lo = 0; long hi = 0; for (int e : L) { if (hi < e) { hi = e; } } hi++; while (lo < hi) { long mid = (lo + hi) / 2; if (mid == 0) { return 0; } long tryK = pieces(L, mid); if (tryK >= k) { lo = mid + 1; } else { hi = mid; } } return lo - 1; } long pieces(vector<int>& L, long len) { int count = 0; for (int e : L) { count += e / len; } return count; } };
相关文章推荐
- 第二周项目3(2) 汉诺塔
- 蘑菇街的IM 网络层
- Socket(一)
- 生活感悟
- Android应用的基本组件介绍
- VEX Context(语言环境)
- 网络协议
- Head First Python 第一章 初始Python
- 2015-09-13
- 穿越两次PIX8.0防火墙并两次静态NAT的FTP测试
- 穿越两次PIX8.0防火墙并两次静态NAT的FTP测试
- C#跨线程调用
- 话说Mac终端下 遇到的千奇百怪的报错及解决方法
- web即时通讯2--基于Spring websocket达到web聊天室
- 从特征描述符到深度学习:计算机视觉发展20年
- C++的函数隐藏、覆盖和重载
- 快速幂取模算法
- 项目管理需要一个在线团队协作平台
- C 双向链表
- [Apple TV 开发教程] 之二 为Apple TV 开发客服端-服务器架构的app