1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-09 09:21:40 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

移除零宽空格

This commit is contained in:
2025-05-25 15:06:02 +08:00
parent 59597532bc
commit 3070bed723
272 changed files with 1031 additions and 749 deletions

View File

@@ -7,9 +7,9 @@
"boundTopicId": 518289,
"title": "Delivering Boxes from Storage to Ports",
"titleSlug": "delivering-boxes-from-storage-to-ports",
"content": "<p>You have the task of delivering some boxes from storage to their ports using only one ship. However, this ship has a <strong>limit</strong> on the <strong>number of boxes</strong> and the <strong>total weight</strong> that it can carry.</p>\n\n<p>You are given an array <code>boxes</code>, where <code>boxes[i] = [ports<sub>i</sub>, weight<sub>i</sub>]</code>, and three integers <code>portsCount</code>, <code>maxBoxes</code>, and <code>maxWeight</code>.</p>\n\n<ul>\n\t<li><code>ports<sub>i</sub></code> is the port where you need to deliver the <code>i<sup>th</sup></code> box and <code>weights<sub>i</sub></code> is the weight of the <code>i<sup>th</sup></code> box.</li>\n\t<li><code>portsCount</code> is the number of ports.</li>\n\t<li><code>maxBoxes</code> and <code>maxWeight</code> are the respective box and weight limits of the ship.</li>\n</ul>\n\n<p>The boxes need to be delivered <strong>in the order they are given</strong>. The ship will follow these steps:</p>\n\n<ul>\n\t<li>The ship will take some number of boxes from the <code>boxes</code> queue, not violating the <code>maxBoxes</code> and <code>maxWeight</code> constraints.</li>\n\t<li>For each loaded box <strong>in order</strong>, the ship will make a <strong>trip</strong> to the port the box needs to be delivered to and deliver it. If the ship is already at the correct port, no <strong>trip</strong> is needed, and the box can immediately be delivered.</li>\n\t<li>The ship then makes a return <strong>trip</strong> to storage to take more boxes from the queue.</li>\n</ul>\n\n<p>The ship must end at storage after all the boxes have been delivered.</p>\n\n<p>Return <em>the <strong>minimum</strong> number of <strong>trips</strong> the ship needs to make to deliver all boxes to their respective ports.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> The optimal strategy is as follows: \n- The ship takes all the boxes in the queue, goes to port 1, then port 2, then port 1 again, then returns to storage. 4 trips.\nSo the total number of trips is 4.\nNote that the first and third boxes cannot be delivered together because the boxes need to be delivered in order (i.e. the second box needs to be delivered at port 2 before the third box).\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> The optimal strategy is as follows: \n- The ship takes the first box, goes to port 1, then returns to storage. 2 trips.\n- The ship takes the second, third and fourth boxes, goes to port 3, then returns to storage. 2 trips.\n- The ship takes the fifth box, goes to port 2, then returns to storage. 2 trips.\nSo the total number of trips is 2 + 2 + 2 = 6.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> The optimal strategy is as follows:\n- The ship takes the first and second boxes, goes to port 1, then returns to storage. 2 trips.\n- The ship takes the third and fourth boxes, goes to port 2, then returns to storage. 2 trips.\n- The ship takes the fifth and sixth boxes, goes to port 3, then returns to storage. 2 trips.\nSo the total number of trips is 2 + 2 + 2 = 6.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= boxes.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= portsCount, maxBoxes, maxWeight &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= ports<sub>i</sub> &lt;= portsCount</code></li>\n\t<li><code>1 &lt;= weights<sub>i</sub> &lt;= maxWeight</code></li>\n</ul>\n",
"content": "<p>You have the task of delivering some boxes from storage to their ports using only one ship. However, this ship has a <strong>limit</strong> on the <strong>number of boxes</strong> and the <strong>total weight</strong> that it can carry.</p>\n\n<p>You are given an array <code>boxes</code>, where <code>boxes[i] = [ports<sub>i</sub>, weight<sub>i</sub>]</code>, and three integers <code>portsCount</code>, <code>maxBoxes</code>, and <code>maxWeight</code>.</p>\n\n<ul>\n\t<li><code>ports<sub>i</sub></code> is the port where you need to deliver the <code>i<sup>th</sup></code> box and <code>weights<sub>i</sub></code> is the weight of the <code>i<sup>th</sup></code> box.</li>\n\t<li><code>portsCount</code> is the number of ports.</li>\n\t<li><code>maxBoxes</code> and <code>maxWeight</code> are the respective box and weight limits of the ship.</li>\n</ul>\n\n<p>The boxes need to be delivered <strong>in the order they are given</strong>. The ship will follow these steps:</p>\n\n<ul>\n\t<li>The ship will take some number of boxes from the <code>boxes</code> queue, not violating the <code>maxBoxes</code> and <code>maxWeight</code> constraints.</li>\n\t<li>For each loaded box <strong>in order</strong>, the ship will make a <strong>trip</strong> to the port the box needs to be delivered to and deliver it. If the ship is already at the correct port, no <strong>trip</strong> is needed, and the box can immediately be delivered.</li>\n\t<li>The ship then makes a return <strong>trip</strong> to storage to take more boxes from the queue.</li>\n</ul>\n\n<p>The ship must end at storage after all the boxes have been delivered.</p>\n\n<p>Return <em>the <strong>minimum</strong> number of <strong>trips</strong> the ship needs to make to deliver all boxes to their respective ports.</em></p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3\n<strong>Output:</strong> 4\n<strong>Explanation:</strong> The optimal strategy is as follows: \n- The ship takes all the boxes in the queue, goes to port 1, then port 2, then port 1 again, then returns to storage. 4 trips.\nSo the total number of trips is 4.\nNote that the first and third boxes cannot be delivered together because the boxes need to be delivered in order (i.e. the second box needs to be delivered at port 2 before the third box).\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> The optimal strategy is as follows: \n- The ship takes the first box, goes to port 1, then returns to storage. 2 trips.\n- The ship takes the second, third and fourth boxes, goes to port 3, then returns to storage. 2 trips.\n- The ship takes the fifth box, goes to port 2, then returns to storage. 2 trips.\nSo the total number of trips is 2 + 2 + 2 = 6.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7\n<strong>Output:</strong> 6\n<strong>Explanation:</strong> The optimal strategy is as follows:\n- The ship takes the first and second boxes, goes to port 1, then returns to storage. 2 trips.\n- The ship takes the third and fourth boxes, goes to port 2, then returns to storage. 2 trips.\n- The ship takes the fifth and sixth boxes, goes to port 3, then returns to storage. 2 trips.\nSo the total number of trips is 2 + 2 + 2 = 6.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= boxes.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= portsCount, maxBoxes, maxWeight &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= ports<sub>i</sub> &lt;= portsCount</code></li>\n\t<li><code>1 &lt;= weights<sub>i</sub> &lt;= maxWeight</code></li>\n</ul>\n",
"translatedTitle": "从仓库到码头运输箱子",
"translatedContent": "<p>你有一辆货运卡车,你需要用这一辆车把一些箱子从仓库运送到码头。这辆卡车每次运输有&nbsp;<strong>箱子数目的限制</strong>&nbsp;和 <strong>总重量的限制</strong>&nbsp;。</p>\n\n<p>给你一个箱子数组&nbsp;<code>boxes</code>&nbsp;和三个整数 <code>portsCount</code>, <code>maxBoxes</code>&nbsp;和&nbsp;<code>maxWeight</code>&nbsp;,其中&nbsp;<code>boxes[i] = [ports<sub>i</sub>, weight<sub>i</sub>]</code>&nbsp;。</p>\n\n<ul>\n\t<li><code>ports<sub>i</sub></code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个箱子需要送达的码头,&nbsp;<code>weights<sub>i</sub></code>&nbsp;是第&nbsp;<code>i</code>&nbsp;个箱子的重量。</li>\n\t<li><code>portsCount</code>&nbsp;是码头的数目。</li>\n\t<li><code>maxBoxes</code> 和&nbsp;<code>maxWeight</code>&nbsp;分别是卡车每趟运输箱子数目和重量的限制。</li>\n</ul>\n\n<p>箱子需要按照 <strong>数组顺序</strong>&nbsp;运输,同时每次运输需要遵循以下步骤:</p>\n\n<ul>\n\t<li>卡车从&nbsp;<code>boxes</code>&nbsp;队列中按顺序取出若干个箱子,但不能违反&nbsp;<code>maxBoxes</code> 和&nbsp;<code>maxWeight</code>&nbsp;限制。</li>\n\t<li>对于在卡车上的箱子,我们需要 <strong>按顺序</strong>&nbsp;处理它们,卡车会通过 <strong>一趟行程</strong>&nbsp;将最前面的箱子送到目的地码头并卸货。如果卡车已经在对应的码头,那么不需要 <strong>额外行程</strong>&nbsp;,箱子也会立马被卸货。</li>\n\t<li>卡车上所有箱子都被卸货后,卡车需要 <strong>一趟行程</strong>&nbsp;回到仓库,从箱子队列里再取出一些箱子。</li>\n</ul>\n\n<p>卡车在将所有箱子运输并卸货后,最后必须回到仓库。</p>\n\n<p>请你返回将所有箱子送到相应码头的&nbsp;<b>最少行程</b>&nbsp;次数。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1</strong></p>\n\n<pre>\n<strong>输入:</strong>boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3\n<b>输出:</b>4\n<b>解释:</b>最优策略如下:\n- 卡车将所有箱子装上车,到达码头 1 ,然后去码头 2 ,然后再回到码头 1 ,最后回到仓库,总共需要 4 趟行程。\n所以总行程数为 4 。\n注意到第一个和第三个箱子不能同时被卸货因为箱子需要按顺序处理也就是第二个箱子需要先被送到码头 2 ,然后才能处理第三个箱子)。\n</pre>\n\n<p><strong>示例 2</strong></p>\n\n<pre>\n<b>输入:</b>boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6\n<b>输出:</b>6\n<b>解释:</b>最优策略如下:\n- 卡车首先运输第一个箱子,到达码头 1 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第二、第三、第四个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第五个箱子,到达码头 2 ,回到仓库,总共 2 趟行程。\n总行程数为 2 + 2 + 2 = 6 。\n</pre>\n\n<p><strong>示例 3</strong></p>\n\n<pre>\n<b>输入:</b>boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7\n<b>输出:</b>6\n<b>解释:</b>最优策略如下:\n- 卡车运输第一和第二个箱子,到达码头 1 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第三和第四个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第五和第六个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。\n总行程数为 2 + 2 + 2 = 6 。\n</pre>\n\n<p><strong>示例 4</strong></p>\n\n<pre>\n<b>输入:</b>boxes = [[2,4],[2,5],[3,1],[3,2],[3,7],[3,1],[4,4],[1,3],[5,2]], portsCount = 5, maxBoxes = 5, maxWeight = 7\n<b>输出:</b>14\n<b>解释:</b>最优策略如下:\n- 卡车运输第一个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第二个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第三和第四个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第五个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第六和第七个箱子,到达码头 3 ,然后去码头 4 ,然后回到仓库,总共 3 趟行程。\n- 卡车运输第八和第九个箱子,到达码头 1 ,然后去码头 5 ,然后回到仓库,总共 3 趟行程。\n总行程数为 2 + 2 + 2 + 2 + 3 + 3 = 14 。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= boxes.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= portsCount, maxBoxes, maxWeight &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= ports<sub>i</sub> &lt;= portsCount</code></li>\n\t<li><code>1 &lt;= weights<sub>i</sub> &lt;= maxWeight</code></li>\n</ul>\n",
"translatedContent": "<p>你有一辆货运卡车,你需要用这一辆车把一些箱子从仓库运送到码头。这辆卡车每次运输有&nbsp;<strong>箱子数目的限制</strong>&nbsp;和 <strong>总重量的限制</strong>&nbsp;。</p>\n\n<p>给你一个箱子数组&nbsp;<code>boxes</code>&nbsp;和三个整数 <code>portsCount</code>, <code>maxBoxes</code>&nbsp;和&nbsp;<code>maxWeight</code>&nbsp;,其中&nbsp;<code>boxes[i] = [ports<sub>i</sub>, weight<sub>i</sub>]</code>&nbsp;。</p>\n\n<ul>\n\t<li><code>ports<sub>i</sub></code>&nbsp;表示第&nbsp;<code>i</code>&nbsp;个箱子需要送达的码头,&nbsp;<code>weights<sub>i</sub></code>&nbsp;是第&nbsp;<code>i</code>&nbsp;个箱子的重量。</li>\n\t<li><code>portsCount</code>&nbsp;是码头的数目。</li>\n\t<li><code>maxBoxes</code> 和&nbsp;<code>maxWeight</code>&nbsp;分别是卡车每趟运输箱子数目和重量的限制。</li>\n</ul>\n\n<p>箱子需要按照 <strong>数组顺序</strong>&nbsp;运输,同时每次运输需要遵循以下步骤:</p>\n\n<ul>\n\t<li>卡车从&nbsp;<code>boxes</code>&nbsp;队列中按顺序取出若干个箱子,但不能违反&nbsp;<code>maxBoxes</code> 和&nbsp;<code>maxWeight</code>&nbsp;限制。</li>\n\t<li>对于在卡车上的箱子,我们需要 <strong>按顺序</strong>&nbsp;处理它们,卡车会通过 <strong>一趟行程</strong>&nbsp;将最前面的箱子送到目的地码头并卸货。如果卡车已经在对应的码头,那么不需要 <strong>额外行程</strong>&nbsp;,箱子也会立马被卸货。</li>\n\t<li>卡车上所有箱子都被卸货后,卡车需要 <strong>一趟行程</strong>&nbsp;回到仓库,从箱子队列里再取出一些箱子。</li>\n</ul>\n\n<p>卡车在将所有箱子运输并卸货后,最后必须回到仓库。</p>\n\n<p>请你返回将所有箱子送到相应码头的&nbsp;<b>最少行程</b>&nbsp;次数。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1</strong></p>\n\n<pre>\n<strong>输入:</strong>boxes = [[1,1],[2,1],[1,1]], portsCount = 2, maxBoxes = 3, maxWeight = 3\n<b>输出:</b>4\n<b>解释:</b>最优策略如下:\n- 卡车将所有箱子装上车,到达码头 1 ,然后去码头 2 ,然后再回到码头 1 ,最后回到仓库,总共需要 4 趟行程。\n所以总行程数为 4 。\n注意到第一个和第三个箱子不能同时被卸货因为箱子需要按顺序处理也就是第二个箱子需要先被送到码头 2 ,然后才能处理第三个箱子)。\n</pre>\n\n<p><strong>示例 2</strong></p>\n\n<pre>\n<b>输入:</b>boxes = [[1,2],[3,3],[3,1],[3,1],[2,4]], portsCount = 3, maxBoxes = 3, maxWeight = 6\n<b>输出:</b>6\n<b>解释:</b>最优策略如下:\n- 卡车首先运输第一个箱子,到达码头 1 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第二、第三、第四个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第五个箱子,到达码头 2 ,回到仓库,总共 2 趟行程。\n总行程数为 2 + 2 + 2 = 6 。\n</pre>\n\n<p><strong>示例 3</strong></p>\n\n<pre>\n<b>输入:</b>boxes = [[1,4],[1,2],[2,1],[2,1],[3,2],[3,4]], portsCount = 3, maxBoxes = 6, maxWeight = 7\n<b>输出:</b>6\n<b>解释:</b>最优策略如下:\n- 卡车运输第一和第二个箱子,到达码头 1 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第三和第四个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第五和第六个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。\n总行程数为 2 + 2 + 2 = 6 。\n</pre>\n\n<p><strong>示例 4</strong></p>\n\n<pre>\n<b>输入:</b>boxes = [[2,4],[2,5],[3,1],[3,2],[3,7],[3,1],[4,4],[1,3],[5,2]], portsCount = 5, maxBoxes = 5, maxWeight = 7\n<b>输出:</b>14\n<b>解释:</b>最优策略如下:\n- 卡车运输第一个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第二个箱子,到达码头 2 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第三和第四个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第五个箱子,到达码头 3 ,然后回到仓库,总共 2 趟行程。\n- 卡车运输第六和第七个箱子,到达码头 3 ,然后去码头 4 ,然后回到仓库,总共 3 趟行程。\n- 卡车运输第八和第九个箱子,到达码头 1 ,然后去码头 5 ,然后回到仓库,总共 3 趟行程。\n总行程数为 2 + 2 + 2 + 2 + 3 + 3 = 14 。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= boxes.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= portsCount, maxBoxes, maxWeight &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= ports<sub>i</sub> &lt;= portsCount</code></li>\n\t<li><code>1 &lt;= weights<sub>i</sub> &lt;= maxWeight</code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 159,