mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-11 18:31:41 +08:00
129 lines
4.0 KiB
HTML
129 lines
4.0 KiB
HTML
<p>给你一个整数 <code>n</code>,表示图中的节点数量,这些节点按从 <code>0</code> 到 <code>n - 1</code> 编号。</p>
|
||
|
||
<p>同时给你一个长度为 <code>n</code> 的整数数组 <code>nums</code>,以及一个整数 <code>maxDiff</code>。</p>
|
||
|
||
<p>如果满足 <code>|nums[i] - nums[j]| <= maxDiff</code>(即 <code>nums[i]</code> 和 <code>nums[j]</code> 的 <strong>绝对差 </strong>至多为 <code>maxDiff</code>),则节点 <code>i</code> 和节点 <code>j</code> 之间存在一条 <strong>无向边 </strong>。</p>
|
||
|
||
<p>此外,给你一个二维整数数组 <code>queries</code>。对于每个 <code>queries[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>,找到节点 <code>u<sub>i</sub></code> 和节点 <code>v<sub>i</sub></code> 之间的 <strong>最短距离 </strong>。如果两节点之间不存在路径,则返回 -1。</p>
|
||
|
||
<p>返回一个数组 <code>answer</code>,其中 <code>answer[i]</code> 是第 <code>i</code> 个查询的结果。</p>
|
||
|
||
<p><strong>注意:</strong>节点之间的边是无权重(unweighted)的。</p>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong class="example">示例 1:</strong></p>
|
||
|
||
<div class="example-block">
|
||
<p><strong>输入:</strong> <span class="example-io">n = 5, nums = [1,8,3,4,2], maxDiff = 3, queries = [[0,3],[2,4]]</span></p>
|
||
|
||
<p><strong>输出:</strong> <span class="example-io">[1,1]</span></p>
|
||
|
||
<p><strong>解释:</strong></p>
|
||
|
||
<p>生成的图如下:</p>
|
||
|
||
<p><img alt="" src="https://pic.leetcode.cn/1745660620-PauXMH-4149example1drawio.png" style="width: 281px; height: 161px;" /></p>
|
||
|
||
<table>
|
||
<tbody>
|
||
<tr>
|
||
<th>查询</th>
|
||
<th>最短路径</th>
|
||
<th>最短距离</th>
|
||
</tr>
|
||
<tr>
|
||
<td>[0, 3]</td>
|
||
<td>0 → 3</td>
|
||
<td>1</td>
|
||
</tr>
|
||
<tr>
|
||
<td>[2, 4]</td>
|
||
<td>2 → 4</td>
|
||
<td>1</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p>因此,输出为 <code>[1, 1]</code>。</p>
|
||
</div>
|
||
|
||
<p><strong class="example">示例 2:</strong></p>
|
||
|
||
<div class="example-block">
|
||
<p><strong>输入:</strong> <span class="example-io">n = 5, nums = [5,3,1,9,10], maxDiff = 2, queries = [[0,1],[0,2],[2,3],[4,3]]</span></p>
|
||
|
||
<p><strong>输出:</strong> <span class="example-io">[1,2,-1,1]</span></p>
|
||
|
||
<p><strong>解释:</strong></p>
|
||
|
||
<p>生成的图如下:</p>
|
||
|
||
<p><img alt="" src="https://pic.leetcode.cn/1745660627-mSVsDs-4149example2drawio.png" style="width: 281px; height: 121px;" /></p>
|
||
|
||
<table>
|
||
<tbody>
|
||
<tr>
|
||
<th>查询</th>
|
||
<th>最短路径</th>
|
||
<th>最短距离</th>
|
||
</tr>
|
||
<tr>
|
||
<td>[0, 1]</td>
|
||
<td>0 → 1</td>
|
||
<td>1</td>
|
||
</tr>
|
||
<tr>
|
||
<td>[0, 2]</td>
|
||
<td>0 → 1 → 2</td>
|
||
<td>2</td>
|
||
</tr>
|
||
<tr>
|
||
<td>[2, 3]</td>
|
||
<td>无</td>
|
||
<td>-1</td>
|
||
</tr>
|
||
<tr>
|
||
<td>[4, 3]</td>
|
||
<td>3 → 4</td>
|
||
<td>1</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
|
||
<p>因此,输出为 <code>[1, 2, -1, 1]</code>。</p>
|
||
</div>
|
||
|
||
<p><strong class="example">示例 3:</strong></p>
|
||
|
||
<div class="example-block">
|
||
<p><strong>输入:</strong> <span class="example-io">n = 3, nums = [3,6,1], maxDiff = 1, queries = [[0,0],[0,1],[1,2]]</span></p>
|
||
|
||
<p><strong>输出:</strong> <span class="example-io">[0,-1,-1]</span></p>
|
||
|
||
<p><strong>解释:</strong></p>
|
||
|
||
<p>由于以下原因,任意两个节点之间都不存在边:</p>
|
||
|
||
<ul>
|
||
<li>节点 0 和节点 1:<code>|nums[0] - nums[1]| = |3 - 6| = 3 > 1</code></li>
|
||
<li>节点 0 和节点 2:<code>|nums[0] - nums[2]| = |3 - 1| = 2 > 1</code></li>
|
||
<li>节点 1 和节点 2:<code>|nums[1] - nums[2]| = |6 - 1| = 5 > 1</code></li>
|
||
</ul>
|
||
|
||
<p>因此,不存在任何可以到达其他节点的节点,输出为 <code>[0, -1, -1]</code>。</p>
|
||
</div>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>1 <= n == nums.length <= 10<sup>5</sup></code></li>
|
||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||
<li><code>0 <= maxDiff <= 10<sup>5</sup></code></li>
|
||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||
<li><code>queries[i] == [u<sub>i</sub>, v<sub>i</sub>]</code></li>
|
||
<li><code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code></li>
|
||
</ul>
|