[LintCode] 183. 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.


The unit of length is centimeter.The length of the woods are all positive integers,you couldn't cut wood into float length.If you couldn't get >= k pieces, return 0.

Example 1

Input:
L = [232, 124, 456]
k = 7
Output: 114
Explanation: We can cut it into 7 pieces if any piece is 114 long, however we can't cut it into 7 pieces if any piece is 115 long.
And for the 124 logs, the excess can be discarded and not used in its entirety.

Example 2

Input:
L = [1, 2, 3]
k = 7
Output: 0
Explanation: It is obvious we can't make it.
Challenge

O(n log Len), where Len is the longest length of the wood.

木材加工

有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k。给定L和k,你需要计算能够得到的小段木头的最大长度。

这道题的思路是二分,而且是在答案上二分。对于所有的木头而言,切割的长度的下界是1,上界是最长的那个木材的长度。在这个范围内进行二分,用每次二分得到的潜在的mid长度再去计算到底能切割成几个小段。因为想使得每段长度尽可能更长,所以 33 行我们先 return right。

时间O(nlogn)

空间O(1)

Java实现

复制代码
 1 public class Solution {
 2     /**
 3      * @param l: Given n pieces of wood with length L[i]
 4      * @param k: An integer
 5      * @return: The maximum length of the small pieces
 6      */
 7     public int woodCut(int[] logs, int k) {
 8         // write your code here
 9         int len = logs.length;
10         long sum = 0;
11         int max = 0;
12         for (int log : logs) {
13             max = Math.max(max, log);
14             sum += log;
15         }
16         // corner case
17         if (sum < k) {
18             return 0;
19         }
20 
21         // normal case
22         int left = 1;
23         int right = max;
24         while (left + 1 < right) {
25             int mid = left + (right - left) / 2;
26             if (isValid(logs, k, mid)) {
27                 left = mid;
28             } else {
29                 right = mid;
30             }
31         }
32         if (isValid(logs, k, right)) {
33             return right;
34         }
35         return left;
36     }
37 
38     private boolean isValid(int[] logs, int k, int mid) {
39         int count = 0;
40         for (int log : logs) {
41             count += log / mid;
42         }
43         if (count >= k) {
44             return true;
45         }
46         return false;
47     }
48 }
复制代码

 

LeetCode 题目总结

posted @   CNoodle  阅读(526)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2021-05-31 [LeetCode] 1167. Minimum Cost to Connect Sticks
2021-05-31 [LeetCode] 968. Binary Tree Cameras
2020-05-31 [LeetCode] 47. Permutations II
2020-05-31 [LeetCode] 77. Combinations
2020-05-31 [LeetCode] 40. Combination Sum II
2020-05-31 [LeetCode] 39. Combination Sum
点击右上角即可分享
微信分享提示