首页 / 资讯中心 / 文章详情

183 · 木材加工(二分法)

183 · 木材加工(二分法) ★ FEATURED ARTICLE
链接LintCode 炼码 - ChatGPT更高效的学习体验题解九章算法 - 帮助更多程序员找到好工作硅谷顶尖IT企业工程师实时在线授课为你传授面试技巧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(vectorint l, int k) { // write your code here if (l.size() 0) { return 0; } int left 0; int right l[0]; for (int i 1; i l.size(); i) { right max(right, l[i]); } while (left 1 right) { int mid left (right-left)/2; if (get_cut_count(l, mid) k) { left mid; } else { right mid; } } return get_cut_count(l, right) k ? right : left; } private: int get_cut_count(std::vectorint l, int count) { int result 0; for (auto i : l) { result i/count; } cout count result endl; return result; } };
阅读完成 · 觉得有帮助?
咨询建站