首页 > 其他分享 >lintcode:Wood Cut

lintcode:Wood Cut

时间:2022-12-01 19:07:49浏览次数:43  
标签:wood Cut return int lintcode mid pieces 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.

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和L[i]中最大的之间。可以用二分查找来寻找最大的划分长度。

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 numCut(vector<int> L,int l){
int num=0;
for(int i=0;i<L.size();i++){
num+=L[i]/l;
}
return num;
}


int woodCut(vector<int> L, int k) {
// write your code here
int res=0;

int max_L=0;
for(int i=0;i<L.size();i++){
if(L[i]>max_L){
max_L=L[i];
}
}

int left=1,right=max_L;

while(left<=right){
int mid=left+(right-left)/2;

int num=numCut(L,mid);

if(num<k){
right=mid-1;
}else{
res=mid;///!!!
left=mid+1;
}
}


return res;
}
};


标签:wood,Cut,return,int,lintcode,mid,pieces,length,Wood
From: https://blog.51cto.com/u_15899184/5903593

相关文章

  • lintcode: Flip Bits
    Determinethenumberofbitsrequiredtoflipifyouwanttoconvertintegerntointegerm.ExampleGivenn=31(11111),m=14(01110),return2.NoteBothn......
  • lintcode: O(1) Check Power of 2
    UsingO(1)timetocheckwhetheranintegernisapowerof2.ExampleForn=4,returntrue;Forn=5,returnfalse;ChallengeO(1)time思路:如果n是2的幂,则n的二进......
  • lintcode: Unique Paths
    Arobotislocatedatthetop-leftcornerofamxngrid(marked‘Start’inthediagrambelow).Therobotcanonlymoveeitherdownorrightatanypointint......
  • lintcode:Trailing Zeros
    15:00StartWriteanalgorithmwhichcomputesthenumberoftrailingzerosinnfactorial.Example11!=39916800,sotheoutshouldbe2ChallengeO(logN)time......
  • lintcode:Update Bits
    Giventwo32-bitnumbers,NandM,andtwobitpositions,iandj.WriteamethodtosetallbitsbetweeniandjinNequaltoM(eg,Mbecomesasubstring......
  • lintcode: Fast Power
    Calculatethea^b%bwherea,bandnareall32bitintegers.ExampleFor231%3=2For1001000%1000=0ChallengeO(logn)思路参见我的博客​​幂模运算​​cla......
  • lintcode:Binary Tree Serialization
    Designanalgorithmandwritecodetoserializeanddeserializeabinarytree.Writingthetreetoafileiscalled‘serialization’andreadingbackfromthe......
  • lintcode: N-Queens
    Then-queenspuzzleistheproblemofplacingnqueensonann×nchessboardsuchthatnotwoqueensattackeachother.Givenanintegern,returnalldistincts......
  • lintcode:Permutations
    Givenalistofnumbers,returnallpossiblepermutations.ChallengeDoitwithoutrecursion.1.递归classSolution{public:/***@paramnums:Alistofi......
  • lintcode:Subsets
    Givenasetofdistinctintegers,returnallpossiblesubsets.ChallengeCanyoudoitinbothrecursivelyanditeratively?1.18sclassSolution{public:/**......