LintCode-木材加工
来源:互联网 发布:excel筛选重复数据 编辑:程序博客网 时间:2024/04/27 13:21
有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k
。当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度。
样例
有3根木头[232, 124, 456]
, k=7
, 最大长度为114
.
注意
木头长度的单位是厘米。原木的长度都是正整数,我们要求切割得到的小段木头的长度也要求是整数。无法切出要求至少 k 段的,则返回 0
即可。
挑战
O(n log Len), Len为 n 段原木中最大的长度
分析:二分查找,注意当数组和小于k时,也就是每1厘米一段都满足不了k段的时候,返回0,这里需要用到long long
bool ok(const vector<int>& L, const int k,const long long len){ long long cnt = 0; for(auto x:L) cnt += x/len; return cnt >= k;}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 if(L.size()==0) return 0; long long sum = 0; for(auto x:L) sum += x; if(sum < k) return 0; long long len = *max_element(L.begin(),L.end()); long long l=1,r=len; while(l<r) { long long mid = (l+r+1)/2; if(ok(L,k,mid)) l = mid; else r = mid-1; } return l; }};
0 0
- LintCode-木材加工
- LintCode 木材加工
- lintcode-木材加工-183
- lintcode 木材加工
- LintCode 木材加工
- LintCode : 木材加工
- LintCode 183 木材加工
- LintCode : 木材加工
- Lintcode 木材加工
- 木材加工-LintCode
- 木材加工
- 木材加工
- 木材加工
- 木材加工
- wikioi 3297 木材加工
- [POJ2774]木材加工
- OpenJugde - 2774:木材加工
- OpenJudge 2774 木材加工
- hdu 5299 ZCC loves strings
- .Net中的早期绑定和后期绑定(二)
- 局部变量在编译时就确定地址吗
- leetcode_Rotate Array
- 学习ASP.NET MVC5框架揭秘笔记-ASP.NET MVC是如何运行的(五)
- LintCode-木材加工
- git-基本知识
- Json转换利器Gson之实例二-Gson注解和GsonBuilder
- HTML中制作两列布局使用浮动
- SQLite应用详解
- 四十八、多态性:虚函数
- 快递物流第三方接口
- Effictive C++读书笔记
- Android核心分析28篇,强烈推荐android初学者,android进阶者看看这个系列教程