1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-03-14 16:22:24 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/过桥的时间 [time-to-cross-a-bridge].html

75 lines
3.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>共有 <code>k</code> 位工人计划将 <code>n</code> 个箱子从右侧的(旧)仓库移动到左侧的(新)仓库。给你两个整数 <code>n</code><code>k</code>,以及一个二维整数数组 <code>time</code> ,数组的大小为 <code>k x 4</code> ,其中 <code>time[i] = [right<sub>i</sub>, pick<sub>i</sub>, left<sub>i</sub>, put<sub>i</sub>]</code></p>
<p>一条河将两座仓库分隔,只能通过一座桥通行。旧仓库位于河的右岸,新仓库在河的左岸。开始时,所有 <code>k</code> 位工人都在桥的左侧等待。为了移动这些箱子,第 <code>i</code> 位工人(下标从 <strong>0</strong> 开始)可以:</p>
<ul>
<li>从左岸(新仓库)跨过桥到右岸(旧仓库),用时 <code>right<sub>i</sub></code> 分钟。</li>
<li>从旧仓库选择一个箱子,并返回到桥边,用时 <code>pick<sub>i</sub></code> 分钟。不同工人可以同时搬起所选的箱子。</li>
<li>从右岸(旧仓库)跨过桥到左岸(新仓库),用时 <code>left<sub>i</sub></code> 分钟。</li>
<li>将箱子放入新仓库,并返回到桥边,用时 <code>put<sub>i</sub></code> 分钟。不同工人可以同时放下所选的箱子。</li>
</ul>
<p>如果满足下面任一条件,则认为工人 <code>i</code><strong>效率低于</strong> 工人 <code>j</code> </p>
<ul>
<li><code>left<sub>i</sub> + right<sub>i</sub> &gt; left<sub>j</sub> + right<sub>j</sub></code></li>
<li><code>left<sub>i</sub> + right<sub>i</sub> == left<sub>j</sub> + right<sub>j</sub></code><code>i &gt; j</code></li>
</ul>
<p>工人通过桥时需要遵循以下规则:</p>
<ul>
<li>同时只能有一名工人过桥。</li>
<li>当桥梁未被使用时,优先让右侧 <strong>效率最低</strong> 的工人(已经拿起盒子的工人)过桥。如果不是,优先让左侧 <strong>效率最低</strong> 的工人通过。</li>
<li>如果左侧已经派出足够的工人来拾取所有剩余的箱子,则 <strong>不会</strong> 再从左侧派出工人。</li>
</ul>
<p><span class="text-only" data-eleid="8" style="white-space: pre;">请你返回最后一个箱子 </span><strong><span class="text-only" data-eleid="9" style="white-space: pre;">到达桥左侧</span></strong><span class="text-only" data-eleid="10" style="white-space: pre;"> 的时间。</span></p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>n = 1, k = 3, time = [[1,1,2,1],[1,1,3,1],[1,1,4,1]]</span></p>
<p><span class="example-io"><b>输出:</b>6</span></p>
<p><b>解释:</b></p>
<pre>
从 0 到 1 分钟:工人 2 通过桥到达右侧。
从 1 到 2 分钟:工人 2 从右侧仓库拿起箱子。
从 2 到 6 分钟:工人 2 通过桥到达左侧。
从 6 到 7 分钟:工人 2 向左侧仓库放下箱子。
整个过程在 7 分钟后结束。我们返回 6 因为该问题要求的是最后一名工人到达桥梁左侧的时间。
</pre>
</div>
<p><strong class="example">示例&nbsp;2</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">n = 3, k = 2, time =</span> [[1,5,1,8],[10,10,10,10]]</p>
<p><b>输出:</b>37</p>
<p><strong>解释:</strong></p>
<pre>
<img src="https://assets.leetcode.com/uploads/2024/11/21/378539249-c6ce3c73-40e7-4670-a8b5-7ddb9abede11.png" style="width: 450px; height: 176px;" />
</pre>
<p>最后一个盒子在37秒时到达左侧。请注意我们并 <strong>没有</strong> 放下最后一个箱子,因为那样会花费更多时间,而且它们已经和工人们一起在左边。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n, k &lt;= 10<sup>4</sup></code></li>
<li><code>time.length == k</code></li>
<li><code>time[i].length == 4</code></li>
<li><code>1 &lt;= left<sub>i</sub>, pick<sub>i</sub>, right<sub>i</sub>, put<sub>i</sub> &lt;= 1000</code></li>
</ul>