{ "data": { "question": { "questionId": "1056", "questionFrontendId": "1011", "boundTopicId": null, "title": "Capacity To Ship Packages Within D Days", "titleSlug": "capacity-to-ship-packages-within-d-days", "content": "

A conveyor belt has packages that must be shipped from one port to another within days days.

\r\n\r\n

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.

\r\n\r\n

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

\r\n\r\n

 

\r\n

Example 1:

\r\n\r\n
\r\nInput: weights = [1,2,3,4,5,6,7,8,9,10], days = 5\r\nOutput: 15\r\nExplanation: A ship capacity of 15 is the minimum to ship all the packages in 5 days like this:\r\n1st day: 1, 2, 3, 4, 5\r\n2nd day: 6, 7\r\n3rd day: 8\r\n4th day: 9\r\n5th day: 10\r\n\r\nNote 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.\r\n
\r\n\r\n

Example 2:

\r\n\r\n
\r\nInput: weights = [3,2,2,4,1,4], days = 3\r\nOutput: 6\r\nExplanation: A ship capacity of 6 is the minimum to ship all the packages in 3 days like this:\r\n1st day: 3, 2\r\n2nd day: 2, 4\r\n3rd day: 1, 4\r\n
\r\n\r\n

Example 3:

\r\n\r\n
\r\nInput: weights = [1,2,3,1,1], days = 4\r\nOutput: 3\r\nExplanation:\r\n1st day: 1\r\n2nd day: 2\r\n3rd day: 3\r\n4th day: 1, 1\r\n
\r\n\r\n

 

\r\n

Constraints:

\r\n\r\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 4141, "dislikes": 96, "isLiked": null, "similarQuestions": "[{\"title\": \"Split Array Largest Sum\", \"titleSlug\": \"split-array-largest-sum\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Divide Chocolate\", \"titleSlug\": \"divide-chocolate\", \"difficulty\": \"Hard\", \"translatedTitle\": null}, {\"title\": \"Cutting Ribbons\", \"titleSlug\": \"cutting-ribbons\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Minimized Maximum of Products Distributed to Any Store\", \"titleSlug\": \"minimized-maximum-of-products-distributed-to-any-store\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "exampleTestcases": "[1,2,3,4,5,6,7,8,9,10]\n5\n[3,2,2,4,1,4]\n3\n[1,2,3,1,1]\n4", "categoryTitle": "Algorithms", "contributors": [], "topicTags": [ { "name": "Array", "slug": "array", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Binary Search", "slug": "binary-search", "translatedName": null, "__typename": "TopicTagNode" }, { "name": "Greedy", "slug": "greedy", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "C++", "langSlug": "cpp", "code": "class Solution {\r\npublic:\r\n int shipWithinDays(vector& weights, int days) {\r\n \r\n }\r\n};", "__typename": "CodeSnippetNode" }, { "lang": "Java", "langSlug": "java", "code": "class Solution {\r\n public int shipWithinDays(int[] weights, int days) {\r\n \r\n }\r\n}", "__typename": "CodeSnippetNode" }, { "lang": "Python", "langSlug": "python", "code": "class Solution(object):\r\n def shipWithinDays(self, weights, days):\r\n \"\"\"\r\n :type weights: List[int]\r\n :type days: int\r\n :rtype: int\r\n \"\"\"", "__typename": "CodeSnippetNode" }, { "lang": "Python3", "langSlug": "python3", "code": "class Solution:\r\n def shipWithinDays(self, weights: List[int], days: int) -> int:", "__typename": "CodeSnippetNode" }, { "lang": "C", "langSlug": "c", "code": "int shipWithinDays(int* weights, int weightsSize, int days){\r\n\r\n}", "__typename": "CodeSnippetNode" }, { "lang": "C#", "langSlug": "csharp", "code": "public class Solution {\r\n public int ShipWithinDays(int[] weights, int days) {\r\n \r\n }\r\n}", "__typename": "CodeSnippetNode" }, { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\r\n * @param {number[]} weights\r\n * @param {number} days\r\n * @return {number}\r\n */\r\nvar shipWithinDays = function(weights, days) {\r\n \r\n};", "__typename": "CodeSnippetNode" }, { "lang": "Ruby", "langSlug": "ruby", "code": "# @param {Integer[]} weights\r\n# @param {Integer} days\r\n# @return {Integer}\r\ndef ship_within_days(weights, days)\r\n \r\nend", "__typename": "CodeSnippetNode" }, { "lang": "Swift", "langSlug": "swift", "code": "class Solution {\r\n func shipWithinDays(_ weights: [Int], _ days: Int) -> Int {\r\n \r\n }\r\n}", "__typename": "CodeSnippetNode" }, { "lang": "Go", "langSlug": "golang", "code": "func shipWithinDays(weights []int, days int) int {\r\n \r\n}", "__typename": "CodeSnippetNode" }, { "lang": "Scala", "langSlug": "scala", "code": "object Solution {\r\n def shipWithinDays(weights: Array[Int], days: Int): Int = {\r\n \r\n }\r\n}", "__typename": "CodeSnippetNode" }, { "lang": "Kotlin", "langSlug": "kotlin", "code": "class Solution {\r\n fun shipWithinDays(weights: IntArray, days: Int): Int {\r\n \r\n }\r\n}", "__typename": "CodeSnippetNode" }, { "lang": "Rust", "langSlug": "rust", "code": "impl Solution {\r\n pub fn ship_within_days(weights: Vec, days: i32) -> i32 {\r\n \r\n }\r\n}", "__typename": "CodeSnippetNode" }, { "lang": "PHP", "langSlug": "php", "code": "class Solution {\r\n\r\n /**\r\n * @param Integer[] $weights\r\n * @param Integer $days\r\n * @return Integer\r\n */\r\n function shipWithinDays($weights, $days) {\r\n \r\n }\r\n}", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function shipWithinDays(weights: number[], days: number): number {\r\n\r\n};", "__typename": "CodeSnippetNode" }, { "lang": "Racket", "langSlug": "racket", "code": "(define/contract (ship-within-days weights days)\r\n (-> (listof exact-integer?) exact-integer? exact-integer?)\r\n\r\n )", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"144.5K\", \"totalSubmission\": \"227.4K\", \"totalAcceptedRaw\": 144513, \"totalSubmissionRaw\": 227413, \"acRate\": \"63.5%\"}", "hints": [ "Binary search on the answer. We need a function possible(capacity) which returns true if and only if we can do the task in D days." ], "solution": null, "status": null, "sampleTestCase": "[1,2,3,4,5,6,7,8,9,10]\n5", "metaData": "{\n \"name\": \"shipWithinDays\",\n \"params\": [\n {\n \"name\": \"weights\",\n \"type\": \"integer[]\"\n },\n {\n \"name\": \"days\",\n \"type\": \"integer\"\n }\n ],\n \"return\": {\n \"type\": \"integer\"\n }\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": true, "envInfo": "{\"cpp\": [\"C++\", \"

Compiled with clang 11 using the latest C++ 17 standard.

\\r\\n\\r\\n

Your code is compiled with level two optimization (-O2). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\"], \"java\": [\"Java\", \"

OpenJDK 17 . Java 8 features such as lambda expressions and stream API can be used.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n

Includes Pair class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.

\"], \"python\": [\"Python\", \"

Python 2.7.12.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\\r\\n\\r\\n

Note that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.

\"], \"c\": [\"C\", \"

Compiled with gcc 8.2 using the gnu99 standard.

\\r\\n\\r\\n

Your code is compiled with level one optimization (-O1). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.

\\r\\n\\r\\n

Most standard library headers are already included automatically for your convenience.

\\r\\n\\r\\n

For hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:

\\r\\n\\r\\n

1. Adding an item to a hash.\\r\\n

\\r\\nstruct hash_entry {\\r\\n    int id;            /* we'll use this field as the key */\\r\\n    char name[10];\\r\\n    UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n    HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

2. Looking up an item in a hash:\\r\\n

\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n    struct hash_entry *s;\\r\\n    HASH_FIND_INT(users, &user_id, s);\\r\\n    return s;\\r\\n}\\r\\n
\\r\\n

\\r\\n\\r\\n

3. Deleting an item in a hash:\\r\\n

\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n    HASH_DEL(users, user);  \\r\\n}\\r\\n
\\r\\n

\"], \"csharp\": [\"C#\", \"

C# 10 with .NET 6 runtime

\\r\\n\\r\\n

Your code is compiled with debug flag enabled (/debug).

\"], \"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use datastructures-js/priority-queue and datastructures-js/queue.

\"], \"ruby\": [\"Ruby\", \"

Ruby 3.1

\\r\\n\\r\\n

Some common data structure implementations are provided in the Algorithms module: https://www.rubydoc.info/github/kanwei/algorithms/Algorithms

\"], \"swift\": [\"Swift\", \"

Swift 5.5.2.

\"], \"golang\": [\"Go\", \"

Go 1.17.6.

\\r\\n\\r\\n

Support https://godoc.org/github.com/emirpasic/gods library.

\"], \"python3\": [\"Python3\", \"

Python 3.10.

\\r\\n\\r\\n

Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.

\\r\\n\\r\\n

For Map/TreeMap data structure, you may use sortedcontainers library.

\"], \"scala\": [\"Scala\", \"

Scala 2.13.7.

\"], \"kotlin\": [\"Kotlin\", \"

Kotlin 1.3.10.

\"], \"rust\": [\"Rust\", \"

Rust 1.58.1

\\r\\n\\r\\n

Supports rand v0.6\\u00a0from crates.io

\"], \"php\": [\"PHP\", \"

PHP 8.1.

\\r\\n

With bcmath module

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"], \"racket\": [\"Racket\", \"

Run with Racket 8.3.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }