Leetcode 1011. Capacity To Ship Packages Within D Days (Binary Search经典)

这篇具有很好参考价值的文章主要介绍了Leetcode 1011. Capacity To Ship Packages Within D Days (Binary Search经典)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  1. Capacity To Ship Packages Within D Days
    Medium
    8.6K
    179
    Companies
    A conveyor belt has packages that must be shipped from one port to another within days days.

The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.

Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within days days.

Example 1:

Input: weights = [1,2,3,4,5,6,7,8,9,10], days = 5
Output: 15
Explanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:
1st day: 1, 2, 3, 4, 5
2nd day: 6, 7
3rd day: 8
4th day: 9
5th day: 10

Note that the cargo must be shipped in the order given, so using a ship of capacity 14 and splitting the packages into parts like (2, 3, 4, 5), (1, 6, 7), (8), (9), (10) is not allowed.
Example 2:

Input: weights = [3,2,2,4,1,4], days = 3
Output: 6
Explanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:
1st day: 3, 2
2nd day: 2, 4
3rd day: 1, 4
Example 3:

Input: weights = [1,2,3,1,1], days = 4
Output: 3
Explanation:
1st day: 1
2nd day: 2
3rd day: 3
4th day: 1, 1

Constraints:

1 <= days <= weights.length <= 5 * 104
1 <= weights[i] <= 500

解法1:binary search
注意:start 从weights的最大值开始,不要从1开始。因为capacity必须>=货物中的最大值。end其实也可以设为所有货物的weights之和。文章来源地址https://www.toymoban.com/news/detail-700749.html

class Solution {
public:
    int shipWithinDays(vector<int>& weights, int days) {
        int n = weights.size();
        int start = *max_element(weights.begin(), weights.end()), end = INT_MAX;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (daysNeeded(weights, mid) <= days) {
                end = mid;
            } else {
                start = mid;
            }
        }
        if (daysNeeded(weights, start) <= days) return start;
        return end;
    }
private:
    int daysNeeded(vector<int>& weights, int capacity) {
        int n = weights.size();
        int res = 0, load = 0;
        for (int i = 0; i < n; i++) {
            load += weights[i];
            if (load > capacity) {
                load = weights[i];
                res++;
            }
        }
        if (load > 0) res++;
        return res;
    }
};

到了这里,关于Leetcode 1011. Capacity To Ship Packages Within D Days (Binary Search经典)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 2409. Count Days Spent Together【前缀和,容斥原理】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2023年04月17日
    浏览(28)
  • 报错 DOTWEEN ► Max Tweens reached: capacity has automatically been increased from 200/50 to 500/50.

    Unity使用DOTween显示UI时产生的报错如下图。 DOTWEEN ► Max Tweens reached: capacity has automatically been increased from 200/50 to 500/50. Use DOTween.SetTweensCapacity to set it manually at startup 报错原因: 由于用同一帧塞满了大量的Tween而发生的产生的报错,导致Tween数的容量溢出的警告。 解决办法: 如

    2024年02月15日
    浏览(26)
  • 解决:RuntimeError: CUDA out of memory. Tried to allocate 160.00 MiB (GPU 0; 10.76 GiB total capacity..

    完整报错:   问题分析: 内存分配不足: 需要160MB,,但GPU只剩下135.31MB。 解决办法: 1.减小batch_size。注意batchsize的调整要配合学习率的调整,一般是正比关系,BS增大两倍,LR增大两倍或者根号二倍。减小也是相应更改。 2.运行torch.cuda.empty_cache()函数。加在训练开始前即可

    2024年02月16日
    浏览(34)
  • leetcode链表题报错 runtime error: member access within null pointer of type ‘ListNode‘

    今天在做leetcode203:移除链表元素时,反复遇到了报错: runtime error: member access within null pointer of type ‘ListNode’ (solution.cpp) ,报错提示的意思是试图访问’ListNode空指针类型的成员,就浅浅记录一下修复bug的过程吧。。。。 刚开始的代码是这样的,逻辑是先建立一个头结点放

    2024年02月11日
    浏览(35)
  • 修复Unity编译时“AAPT2 aapt2-4.1.2-6503028-osx Daemon #0 Failed to shutdown within timeout”错误

    要解决这个报错首先我们要了解AAPT是什么东西。 aapt 全称为 Android Asset Packaging Tool,即为Android资源打包工具。作为unity开发人员,一般跟Android打交道比较少,感兴趣的同学可以先去官网学习学习。 ##aapt2版本 首先4.1.2-6503028是使用appt2 sdk版本。 我们可以在maven仓库查到具体版

    2024年02月16日
    浏览(28)
  • ERROR util.Shell: Failed to locate the winutils binary in the hadoop binary path

    让我们静下心来审视一下自己,是不是忙得有价值,忙得有意义,忙得有目的,看一看我们是不是因为忙而迷失了自己。如果我们仅是为了忙而忙,那不妨让自己停一下疲于奔命的脚步,用心体味一下生活,你会发现生活中未被发掘的美。 这个错误通常出现在使用Hadoop时,因

    2024年02月03日
    浏览(32)
  • LeetCode //C - 124. Binary Tree Maximum Path Sum

    A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node’s values in the path. Given the root of a binary tree, return the maximum

    2024年02月09日
    浏览(30)
  • LeetCode //C - 199. Binary Tree Right Side View

    Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.   Example 1: Input: root = [1,2,3,null,5,null,4] Output: [1,3,4] Example 2: Input: root = [1,null,3] Output: [1,3] Example 3: Input root = [] Output [] Constraints: The number of nodes in the tree is in t

    2024年01月20日
    浏览(27)
  • Leetcode 1367. Linked List in Binary Tree (二叉树好题)

    Linked List in Binary Tree Medium Given a binary tree root and a linked list with head as the first node. Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False. In this context downward path means a path that starts at some node and goes downwards. Exampl

    2024年01月25日
    浏览(36)
  • leetcode分类刷题:二分查找(Binary Search)(四、基于值域的数组/矩阵类型)

    基于值域的二分法与基于定义域的题型不同,它的目标是从一“ 特殊排序序列 ”中确定“第k个元素值”,而不像基于定义域的题型是从排序序列中找小于等于特定target值的第一个索引;同时,针对“特殊排序序列”,往往需要 嵌套使用双指针 法进行操作,进一步增加了对

    2024年02月11日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包