ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LintCode] 183. Wood Cut

2022-05-31 08:01:39  阅读:186  来源: 互联网

标签:Cut return logs int mid pieces 183 length Wood


 

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长度再去计算到底能切割成几个小段。

时间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 题目总结

标签:Cut,return,logs,int,mid,pieces,183,length,Wood
来源: https://www.cnblogs.com/cnoodle/p/16329100.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有