183. 木材加工

183. 木材加工
 

有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目至少为 k。当然,我们希望得到的小段越长越好,你需要计算能够得到的小段木头的最大长度。
样例

Example 1

Input:
L = [232, 124, 456]
k = 7
Output: 114
Explanation: We can cut it into 7 pieces if any piece is 114cm long, however we can't cut it into 7 pieces if any piece is 115cm long.

Example 2

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

挑战

O(n log Len), Len为 n 段原木中最大的长度
注意事项

木头长度的单位是厘米。原木的长度都是正整数,我们要求切割得到的小段木头的长度也要求是整数。无法切出要求至少 k 段的,则返回 0 即可。

int woodCut(vector<int> &L, int k) {
        // write your code here
        
    if(L.empty())
        return 0;
       int minInt = INT_MAX;
    int maxInt = INT_MIN;

    for (auto it : L)
    {
        if (it< maxInt)
        {
            maxInt = it;
        }
    }


    long long  sum = 0;
    for (auto it : L)
    {
        sum += it;
    }
    
    if(sum < k)
        return 0;
    
    if (k>0)
        minInt = sum / k;

    int retMax;
    for (int i = minInt; i>=1; i--)
    {
        int kSum = 0;
        for (auto it : L)
        {
            kSum += (it / i);
        }
        if (kSum >= k)
        {
            retMax = i;
            break;
        }
    }

    return retMax;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值