• [每日一题]leetcode 1011. 在 D 天内送达包裹的能力


    抽象为把序列分成D段,求和最大的段的最小值

    下界为最大值,上界为序列和,二分结果,每次去验证是否合适即可

    class Solution {
    public:
    
        int shipWithinDays(vector<int>& weights, int D) {
            int total = 0;
            int max_v = weights[0];
            for(int i = 0; i < weights.size(); i++) max_v = max(max_v, weights[i]), total += weights[i];
            int x = max_v, y = total;
            int cnt = 1, cnt_s;
            while(x < y)
            {
                cnt_s = 0;
                cnt = 1;
                int mid = (x + y) / 2;
                int i;
                for(i = 0; i < weights.size(); i++)
                    if(cnt_s + weights[i] <= mid) cnt_s += weights[i];
                    else cnt_s = weights[i], cnt++; 
                if(cnt > D) x = mid + 1;
                else y = mid;
            }
            return x;
    
    
        }
    };
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    shell
    regionMatches方法
    ==
    使用INTO子句创建新表
    数据库除运算
    数据库笛卡尔积运算
    人生格言
    刚开通~
    Nginx:413 Request Entity Too Large
    ORACLE 查看并修改最大连接数
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/14705822.html
Copyright © 2020-2023  润新知