Lintcode: 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.

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.

注意是在[1,max(L)]里面找

 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[] L, int k) {
 8         // write your code here
 9         if (L==null || L.length==0 || k<=0) return 0;
10         Arrays.sort(L);
11         int l=1, r=L[L.length-1];
12         while (l <= r) {
13             int m = l+(r-l)/2;
14             if (calc(L, m)>=k) l=m+1;
15             else r=m-1;
16         }
17         return r;
18     }
19
20     public int calc(int[] L, int len) {
21         int res = 0;
22         for (int i=0; i<L.length; i++) {
23             res += L[i]/len;
24         }
25         return res;
26     }
27 }
时间: 2024-07-30 10:14:28

Lintcode: Wood Cut的相关文章

[Lintcode]183. Wood Cut

183. Wood Cut 本题难度: Hard Topic: Binary Search Description 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

Wood Cut

http://www.lintcode.com/zh-cn/problem/wood-cut/ 有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k.当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度. 注意事项 木头长度的单位是厘米.原木的长度都是正整数,我们要求切割得到的小段木头的长度也要求是整数.无法切出要求至少 k 段的,则返回 0 即可. Lintcode上面的一道题目,很有意思,光看题可能想象不出是用二分来做.但是实际小段的长

刷题--二分法(4)

最后一种二分法的可能情况:最终的答案是二分的.也就是说答案是处在sort array中的,需要每次验证一下mid对应的值是偏大还是偏小. 例  lintcode 183. Wood Cut   https://www.lintcode.com/problem/wood-cut/description 从考虑答案的角度,切割长度从1开始增加,长度为1的时候可能切出几百根,为2时几百根,随着长度增加,能切出的根数逐渐减小,直到到达最小的 大于等于k 的根数,即找对应根数大于等于k的最后一个位置,所以

Bug Free

1 Search Insert Position---NOT BUG FREE 1 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 2 3 You may assume no duplicates in the array. 注意插入位置

Binary Search 二分查找总结

Binary Search基本的复杂度为O(logn).如果提示需要对O(n)的算法进行优化,非常可能就是二分,另外二分一般出现在排序数组或者变形后的排序数组(rotated array)当中.二分主要有两种,binary search on index(index上的二分)和binary search on result(结果上的二分).index上的二分主要有 result上的二分主要有Sqrt(x),Wood cut两种. 另外binary search的版本很多,区别在终结条件,比如是l

CS3K.com 九章算法强化班

Advanced Data Structure -- Union Find Number of Islands 题目 思路I:BFS 避免相同位置的元素重复入列,访问过的元素都要标记为已访问.BFS为了得到下一个点的坐标,所以需要建立一个表示位置的坐标类. 思路II:并查集 等价于求集合中连通块的个数. Number of Islands II 题目 思路:并查集,把二维数组转化为一维father数组. LeetCode 547. Friend Circles 题目 思路:并查集的典型应用.题目

red oak plywood- plain sliced and rotary cut myav

<img title="red oak plywood- plain sliced and rotary cut" alt="red oak plywood- plain sliced and rotary cut" src="http://s12.sinaimg.cn/bmiddle/507a9ec8g70eb53f7624b&690" real_src="http://s12.sinaimg.cn/bmiddle/50

LintCode Subtree

原题链接在这里:http://www.lintcode.com/en/problem/subtree/ You have two every large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree ofT1. Have you met this question in a real intervi

HDU5203 Rikka with wood sticks

Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: Yuta have a wood stick of length n which consists of n linked sticks of length 1. So it