{"id":597,"date":"2025-07-10T12:31:55","date_gmt":"2025-07-10T07:01:55","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=597"},"modified":"2025-07-10T12:31:57","modified_gmt":"2025-07-10T07:01:57","slug":"capacity-to-ship-packages-within-d-days","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/","title":{"rendered":"Capacity To Ship Packages Within D Days | Leetcode 1011"},"content":{"rendered":"\n<p>The &#8220;Capacity To Ship\u00a0Packages Within\u00a0D Days&#8221; problem\u00a0is a classic\u00a0binary search\u00a0question that\u00a0helps you practice\u00a0optimization\u00a0with constraints. <\/p>\n\n\n\n<p>In this blog, we\u2019ll explain\u00a0the problem, walk\u00a0you through both\u00a0brute force and\u00a0optimal solutions, and make everything\u00a0easy to understand\u00a0with code comments, dry runs, and\u00a0clear explanations.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/capacity-to-ship-packages-within-d-days\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-problem-statement-\"><strong>Problem Statement<\/strong><\/h2>\n\n\n\n<p>You are given an array&nbsp;<code>weights<\/code>&nbsp;where each element represents the weight of a package. You have&nbsp;<code>D<\/code>&nbsp;days to ship all the packages.<br>You must ship the packages in order (no reordering). Each day, you can ship as many packages as you want, as long as their total weight does not exceed the ship&#8217;s capacity.<br>Your task is to find the&nbsp;<strong>minimum ship capacity<\/strong>&nbsp;needed to ship all packages within&nbsp;<code>D<\/code>&nbsp;days.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-example-1-\"><strong>Example 1<\/strong><\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>weights = [1], D = 5<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><br><code>15<\/code><\/p>\n\n\n\n<p><strong>Explanation:<\/strong><br>A capacity of 15 allows the packages to be shipped in 5 days.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-example-2-\"><strong>Example 2<\/strong><\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>weights = [1], D = 3<\/code><\/p>\n\n\n\n<p><strong>Output:<\/strong><br><code>6<\/code><\/p>\n\n\n\n<p><strong>Explanation:<\/strong><br>A capacity of 6 allows the packages to be shipped in 3 days.<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-667986be-2b88-4544-ba52-e23ff9048627\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">hide<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#0-problem-statement-\" style=\"\">Problem Statement<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#1-example-1-\" style=\"\">Example 1<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#2-example-2-\" style=\"\">Example 2<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#3-brute-force-solution-try-every-possible-capacity\" style=\"\">Brute Force Solution (Try Every Possible Capacity)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#4-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#5-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#6-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#7-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#8-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#9-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#10-optimal-solution-binary-search\" style=\"\">Optimal Solution (Binary Search)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#11-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#12-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#13-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#14-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#15-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#16-conclusion\" style=\"\">Conclusion<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/capacity-to-ship-packages-within-d-days\/#17-final-thoughts\" style=\"\">Final Thoughts<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-brute-force-solution-try-every-possible-capacity\">Brute Force Solution (Try Every Possible Capacity)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>Let\u2019s solve the problem in the most straightforward way:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Try Every Capacity:<\/strong><br>Try every possible ship capacity from the maximum single package weight to the sum of all package weights.<\/li>\n\n\n\n<li><strong>Check If It Meets the Days Constraint:<\/strong><br>For each capacity, simulate the shipping process and count how many days it would take.<\/li>\n\n\n\n<li><strong>Return the First Valid Capacity:<\/strong><br>The first capacity for which the total days is less than or equal to\u00a0<code>D<\/code>\u00a0is our answer.<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>We check every possible capacity, so we\u2019re sure to find the minimum one that works.<\/li>\n<\/ol>\n\n\n\n<p>This approach is easy to understand but not efficient for large inputs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\n\nclass Solution:\n    def find_days(self, weights, capacity):\n        total_days = 1\n        current_load = 0\n\n        for weight in weights:\n            if current_load + weight &gt; capacity:\n                total_days += 1\n                current_load = 0\n            current_load += weight\n\n        return total_days\n\n    def shipWithinDays(self, weights: List[int], days: int) -&gt; int:\n        start_weight = max(weights)            # Minimum possible capacity\n        end_weight = sum(weights)              # Maximum possible capacity\n        for w in range(start_weight, end_weight + 1):\n            total_days_taken = self.find_days(weights, w)\n            if total_days_taken &lt;= days:       # Check if this capacity works\n                return w                       # Return the minimum capacity found\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">from<\/span><span style=\"color: #E1E4E8\"> typing <\/span><span style=\"color: #F97583\">import<\/span><span style=\"color: #E1E4E8\"> List<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">find_days<\/span><span style=\"color: #E1E4E8\">(self, weights, capacity):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        total_days <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        current_load <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> weight <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> weights:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> current_load <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> weight <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> capacity:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                total_days <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                current_load <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            current_load <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> weight<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> total_days<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">shipWithinDays<\/span><span style=\"color: #E1E4E8\">(self, weights: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">], days: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        start_weight <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(weights)            <\/span><span style=\"color: #6A737D\"># Minimum possible capacity<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        end_weight <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">sum<\/span><span style=\"color: #E1E4E8\">(weights)              <\/span><span style=\"color: #6A737D\"># Maximum possible capacity<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> w <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">range<\/span><span style=\"color: #E1E4E8\">(start_weight, end_weight <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            total_days_taken <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.find_days(weights, w)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> total_days_taken <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> days:       <\/span><span style=\"color: #6A737D\"># Check if this capacity works<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> w                       <\/span><span style=\"color: #6A737D\"># Return the minimum capacity found<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For each capacity from the largest single package to the total weight, simulate the shipping process.<\/li>\n\n\n\n<li>Use the helper function\u00a0<code>find_days<\/code>\u00a0to count how many days are needed for a given capacity.<\/li>\n\n\n\n<li>Return the first capacity that allows all packages to be shipped within\u00a0<code>D<\/code>\u00a0days.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>weights = [1], D = 5<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Try capacity = 10: Too many days needed.<\/li>\n\n\n\n<li>Try capacity = 15: Can ship in 5 days. Return 15.<\/li>\n<\/ul>\n\n\n\n<p><strong>Final Output:<\/strong><br><code>15<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O((sum(weights) &#8211; max(weights)) * n)<br>For each capacity, we check all packages.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The brute force approach is simple and works for small inputs, but it\u2019s too slow for large values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-optimal-solution-binary-search\">Optimal Solution (Binary Search)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<p>We can do much better using binary search:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Binary Search on Capacity:<\/strong><br>The answer lies between the largest single package and the total weight. Use binary search to find the minimum capacity.<\/li>\n\n\n\n<li><strong>Check If It Meets the Days Constraint:<\/strong><br>For each mid value (capacity), simulate the shipping process and count the days needed.<\/li>\n\n\n\n<li><strong>Adjust Search Window:<\/strong>\n<ul class=\"wp-block-list\">\n<li>If the number of days is within\u00a0<code>D<\/code>, try a smaller capacity.<\/li>\n\n\n\n<li>If not, try a larger capacity.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Why does this work?<\/strong><br>The days needed function is monotonic: as the capacity increases, the days needed decreases. Binary search is perfect for this.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#e1e4e8;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#24292e\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"from typing import List\n\nclass Solution:\n    def find_days(self, weights, capacity):\n        total_days = 1\n        current_load = 0\n\n        for weight in weights:\n            if current_load + weight &gt; capacity:\n                total_days += 1\n                current_load = 0\n            current_load += weight\n\n        return total_days\n\n    def shipWithinDays(self, weights: List[int], days: int) -&gt; int:\n        low = max(weights)              # Minimum possible capacity\n        high = sum(weights)             # Maximum possible capacity\n        while low &lt;= high:\n            mid = (low + high) \/\/ 2\n            numberOfDays = self.find_days(weights, mid)\n            if numberOfDays &lt;= days:\n                high = mid - 1          # Try a smaller capacity\n            else:\n                low = mid + 1           # Need a larger capacity\n        return low                      # The minimum capacity found\" style=\"color:#e1e4e8;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-dark\" style=\"background-color: #24292e\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F97583\">from<\/span><span style=\"color: #E1E4E8\"> typing <\/span><span style=\"color: #F97583\">import<\/span><span style=\"color: #E1E4E8\"> List<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F97583\">class<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">Solution<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">find_days<\/span><span style=\"color: #E1E4E8\">(self, weights, capacity):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        total_days <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        current_load <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">for<\/span><span style=\"color: #E1E4E8\"> weight <\/span><span style=\"color: #F97583\">in<\/span><span style=\"color: #E1E4E8\"> weights:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> current_load <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> weight <\/span><span style=\"color: #F97583\">&gt;<\/span><span style=\"color: #E1E4E8\"> capacity:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                total_days <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                current_load <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            current_load <\/span><span style=\"color: #F97583\">+=<\/span><span style=\"color: #E1E4E8\"> weight<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> total_days<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">    <\/span><span style=\"color: #F97583\">def<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #B392F0\">shipWithinDays<\/span><span style=\"color: #E1E4E8\">(self, weights: List[<\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">], days: <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">) -&gt; <\/span><span style=\"color: #79B8FF\">int<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">max<\/span><span style=\"color: #E1E4E8\">(weights)              <\/span><span style=\"color: #6A737D\"># Minimum possible capacity<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">sum<\/span><span style=\"color: #E1E4E8\">(weights)             <\/span><span style=\"color: #6A737D\"># Maximum possible capacity<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">while<\/span><span style=\"color: #E1E4E8\"> low <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> high:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            mid <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> (low <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> high) <\/span><span style=\"color: #F97583\">\/\/<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            numberOfDays <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">self<\/span><span style=\"color: #E1E4E8\">.find_days(weights, mid)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">if<\/span><span style=\"color: #E1E4E8\"> numberOfDays <\/span><span style=\"color: #F97583\">&lt;=<\/span><span style=\"color: #E1E4E8\"> days:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                high <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">-<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">          <\/span><span style=\"color: #6A737D\"># Try a smaller capacity<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">            <\/span><span style=\"color: #F97583\">else<\/span><span style=\"color: #E1E4E8\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">                low <\/span><span style=\"color: #F97583\">=<\/span><span style=\"color: #E1E4E8\"> mid <\/span><span style=\"color: #F97583\">+<\/span><span style=\"color: #E1E4E8\"> <\/span><span style=\"color: #79B8FF\">1<\/span><span style=\"color: #E1E4E8\">           <\/span><span style=\"color: #6A737D\"># Need a larger capacity<\/span><\/span>\n<span class=\"line\"><span style=\"color: #E1E4E8\">        <\/span><span style=\"color: #F97583\">return<\/span><span style=\"color: #E1E4E8\"> low                      <\/span><span style=\"color: #6A737D\"># The minimum capacity found<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#24292e;color:#d3d7dd;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13-code-explanation\">Code Explanation<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use binary search between the largest package and the total weight.<\/li>\n\n\n\n<li>For each mid capacity, simulate the shipping process.<\/li>\n\n\n\n<li>If the number of days is within\u00a0<code>D<\/code>, try a smaller capacity.<\/li>\n\n\n\n<li>If not, try a larger capacity.<\/li>\n\n\n\n<li>Return the minimum valid capacity found.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14-dry-run\">Dry Run<\/h3>\n\n\n\n<p><strong>Input:<\/strong><br><code>weights = [1], D = 5<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>low = 10, high = 55<\/li>\n\n\n\n<li>mid = 32, days needed = 3 (try smaller, high = 31)<\/li>\n\n\n\n<li>mid = 20, days needed = 4 (try smaller, high = 19)<\/li>\n\n\n\n<li>mid = 14, days needed = 6 (too many days, low = 15)<\/li>\n\n\n\n<li>mid = 17, days needed = 5 (just right, try smaller, high = 16)<\/li>\n\n\n\n<li>mid = 15, days needed = 5 (just right, try smaller, high = 14)<\/li>\n<\/ul>\n\n\n\n<p>Loop ends, answer is 15.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n * log(sum(weights) &#8211; max(weights)))<br>Binary search on capacity, checking all packages for each guess.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1)<br>Only variables for counting.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16-conclusion\">Conclusion<\/h3>\n\n\n\n<p>The optimal solution is efficient and works well for large arrays and high values. It\u2019s the best approach for interviews and practical use.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-final-thoughts\">Final Thoughts<\/h2>\n\n\n\n<p>The &#8220;Capacity To Ship Packages Within D Days&#8221; problem is a great example of how to optimize from brute force to an efficient binary search solution. Start with brute force to understand the basics, then use binary search for best performance.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The &#8220;Capacity To Ship\u00a0Packages Within\u00a0D Days&#8221; problem\u00a0is a classic\u00a0binary search\u00a0question that\u00a0helps you practice\u00a0optimization\u00a0with constraints. In this blog, we\u2019ll explain\u00a0the problem, walk\u00a0you through both\u00a0brute force and\u00a0optimal solutions, and make everything\u00a0easy to understand\u00a0with code comments, dry runs, and\u00a0clear explanations. Here&#8217;s the [Problem Link] to begin with. Problem Statement You are given an array&nbsp;weights&nbsp;where each element represents the<\/p>\n","protected":false},"author":1,"featured_media":600,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[28,19],"class_list":{"0":"post-597","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-binary-search-on-answers","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/capacity-to-ship-packages-within-D-days-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=597"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/597\/revisions"}],"predecessor-version":[{"id":602,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/597\/revisions\/602"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/600"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}