[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 can get from the n pieces of wood? Given L & k, return the maximum length of the small pieces.

Example

For L=[232, 124, 456], k=7, return 114.

Challenge

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

Notice

You couldn‘t cut wood into float length.

If you couldn‘t get >= k pieces, return 0.

我的代码

class Solution:
    """
    @param L: Given n pieces of wood with length L[i]
    @param k: An integer
    @return: The maximum length of the small pieces
    """
    def woodCut(self, L, k):
        # write your code here
     # write your code here
        if L == []:
            return 0
        L.sort()
        max = L[-1]
        l = 1
        r = L[-1]
        while(l<=r):
            count = 0
            m = (l + r) // 2
            print(l,r,m)
            for i in L:
                count += i//m
            if count>=k:
                l = m+1
            else:
                r = m-1
        #1.边界问题,最后结果可能是从右向左逼近,此时m为第一个不满足条件的情况,反之,从左往右逼近,则m为满足条件的情况
        print(m)
        count = 0
        for i in L:
            count += i//m
        print(count)
        if count >= k:
            return m
        else:
            return m-1

思路

  1. 切割的大小肯定小于最长木料长度,而从最长木料选择截取特定长的的时间复杂度是log(L)
  2. 共有n块木料,每个特定的长度要试n次
  • 时间复杂度: nlog(L) L为最长木料长度
  • 出错

    想得挺顺利的,就是一个边界问题没考虑好,最后结果可能是从右向左逼近,此时m为第一个不满足条件的情况,反之,从左往右逼近,则m为满足条件的情况

    也有低级错误

[Lintcode]183. Wood Cut

原文地址:https://www.cnblogs.com/siriusli/p/10358600.html

时间: 2024-08-04 11:07:41

[Lintcode]183. Wood Cut的相关文章

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 th

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

脑洞大开加偏执人格——可持久化treap版的Link Cut Tree2

试了一下先上再下的Treap方式,很高兴,代码变短了,但是,跑的变慢了!!!其实慢得不多,5%左右.而且这个版本的写法不容易写错..只要会一般可持久化Treap的人写着都不难...就是相对于(压行的)Splay还是长了点... 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <algorithm> 5 6 using namespace std; 7