|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 简单 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/4000-4099/4024.Nearest%20Available%20Drone/README.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [4024. 最近的可用无人机](https://leetcode.cn/problems/nearest-available-drone) |
| 10 | + |
| 11 | +[English Version](/solution/4000-4099/4024.Nearest%20Available%20Drone/README_EN.md) |
| 12 | + |
| 13 | +## 题目描述 |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p>给你一个二维整数数组 <code>drones</code>,其中 <code>drones[i] = [x<sub>i</sub>, y<sub>i</sub>, range<sub>i</sub>]</code> 表示第 <code>i<sup>th</sup></code> 架无人机的横坐标、纵坐标和飞行范围。</p> |
| 18 | + |
| 19 | +<p>另给你一个整数数组 <code>target = [tx, ty]</code>,表示目标的坐标。</p> |
| 20 | + |
| 21 | +<p>如果无人机 <code>drones[i]</code> 的坐标与目标坐标之间的<strong>曼哈顿距离</strong><strong>小于或等于</strong>其 <code>range<sub>i</sub></code>,则该无人机能够到达目标。</p> |
| 22 | + |
| 23 | +<p>返回能够到达目标且与目标之间<strong>曼哈顿距离最小</strong>的无人机的<strong>下标</strong>。如果存在多个符合条件的无人机,则返回其中<strong>最小的下标</strong>。如果没有无人机能够到达目标,则返回 -1。</p> |
| 24 | + |
| 25 | +<p>两个坐标 <code>(x<sub>i</sub>, y<sub>i</sub>)</code> 和 <code>(x<sub>j</sub>, y<sub>j</sub>)</code> 之间的<strong>曼哈顿距离</strong>为 <code>|x<sub>i</sub> - x<sub>j</sub>| + |y<sub>i</sub> - y<sub>j</sub>|</code>。</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | + |
| 29 | +<p><strong class="example">示例 1:</strong></p> |
| 30 | + |
| 31 | +<div class="example-block"> |
| 32 | +<p><strong>输入:</strong> <span class="example-io">drones = [[0,0,8],[2,2,9]], target = [3,4]</span></p> |
| 33 | + |
| 34 | +<p><strong>输出:</strong> <span class="example-io">1</span></p> |
| 35 | + |
| 36 | +<p><strong>解释:</strong></p> |
| 37 | + |
| 38 | +<ul> |
| 39 | + <li><code>drones[0]</code> 与 <code>target</code> 之间的距离为 <code>|0 - 3| + |0 - 4| = 7</code>,没有超出其飞行范围 8。</li> |
| 40 | + <li><code>drones[1]</code> 与 <code>target</code> 之间的距离为 <code>|2 - 3| + |2 - 4| = 3</code>,没有超出其飞行范围 9。</li> |
| 41 | + <li>由于 <code>drones[1]</code> 是距离目标最近的无人机,因此答案为 1。</li> |
| 42 | +</ul> |
| 43 | +</div> |
| 44 | + |
| 45 | +<p><strong class="example">示例 2:</strong></p> |
| 46 | + |
| 47 | +<div class="example-block"> |
| 48 | +<p><strong>输入:</strong> <span class="example-io">drones = [[2,1,5],[4,4,5],[6,6,8]], target = [5,5]</span></p> |
| 49 | + |
| 50 | +<p><strong>输出:</strong> <span class="example-io">1</span></p> |
| 51 | + |
| 52 | +<p><strong>解释:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><code>drones[0]</code> 与 <code>target</code> 之间的距离为 <code>|2 - 5| + |1 - 5| = 7</code>,大于其飞行范围 5。</li> |
| 56 | + <li><code>drones[1]</code> 与 <code>target</code> 之间的距离为 <code>|4 - 5| + |4 - 5| = 2</code>,没有超出其飞行范围 5。</li> |
| 57 | + <li><code>drones[2]</code> 与 <code>target</code> 之间的距离为 <code>|6 - 5| + |6 - 5| = 2</code>,没有超出其飞行范围 8。</li> |
| 58 | + <li><code>drones[1]</code> 和 <code>drones[2]</code> 都是距离目标最近的无人机。由于需要返回最小下标,因此答案为 1。</li> |
| 59 | +</ul> |
| 60 | +</div> |
| 61 | + |
| 62 | +<p><strong class="example">示例 3:</strong></p> |
| 63 | + |
| 64 | +<div class="example-block"> |
| 65 | +<p><strong>输入:</strong> <span class="example-io">drones = [[4,4,5]], target = [8,6]</span></p> |
| 66 | + |
| 67 | +<p><strong>输出:</strong> <span class="example-io">-1</span></p> |
| 68 | + |
| 69 | +<p><strong>解释:</strong></p> |
| 70 | + |
| 71 | +<ul> |
| 72 | + <li><code>drones[0]</code> 与 <code>target</code> 之间的距离为 <code>|4 - 8| + |4 - 6| = 6</code>,大于其飞行范围 5。</li> |
| 73 | + <li>没有无人机能够到达目标,因此答案为 -1。</li> |
| 74 | +</ul> |
| 75 | +</div> |
| 76 | + |
| 77 | +<p> </p> |
| 78 | + |
| 79 | +<p><strong>提示:</strong></p> |
| 80 | + |
| 81 | +<ul> |
| 82 | + <li><code>1 <= drones.length <= 100</code></li> |
| 83 | + <li><code>drones[i] = [x<sub>i</sub>, y<sub>i</sub>, range<sub>i</sub>]</code></li> |
| 84 | + <li><code>target = [tx, ty]</code></li> |
| 85 | + <li><code>-25 <= x<sub>i</sub>, y<sub>i</sub>, tx, ty <= 25</code></li> |
| 86 | + <li><code>1 <= range<sub>i</sub> <= 100</code></li> |
| 87 | +</ul> |
| 88 | + |
| 89 | +<!-- description:end --> |
| 90 | + |
| 91 | +## 解法 |
| 92 | + |
| 93 | +<!-- solution:start --> |
| 94 | + |
| 95 | +### 方法一:遍历 |
| 96 | + |
| 97 | +我们遍历每一架无人机,计算其与目标的曼哈顿距离 $d = |x_i - t_x| + |y_i - t_y|$。若 $d \le \textit{range}_i$,则该无人机可达。在所有可达无人机中,选择距离最小的一架;若距离相同,由于我们从左到右遍历且仅在距离严格更小时更新答案,因此会自动保留更小的下标。若没有可达无人机,返回 $-1$。 |
| 98 | + |
| 99 | +时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是无人机的数量。 |
| 100 | + |
| 101 | +<!-- tabs:start --> |
| 102 | + |
| 103 | +#### Python3 |
| 104 | + |
| 105 | +```python |
| 106 | +class Solution: |
| 107 | + def nearestDrone(self, drones: list[list[int]], target: list[int]) -> int: |
| 108 | + ans = -1 |
| 109 | + mn = inf |
| 110 | + tx, ty = target |
| 111 | + for i, (x, y, r) in enumerate(drones): |
| 112 | + d = abs(x - tx) + abs(y - ty) |
| 113 | + if d <= r and mn > d: |
| 114 | + ans = i |
| 115 | + mn = d |
| 116 | + return ans |
| 117 | +``` |
| 118 | + |
| 119 | +#### Java |
| 120 | + |
| 121 | +```java |
| 122 | +class Solution { |
| 123 | + public int nearestDrone(int[][] drones, int[] target) { |
| 124 | + int ans = -1; |
| 125 | + int mn = Integer.MAX_VALUE; |
| 126 | + int tx = target[0], ty = target[1]; |
| 127 | + |
| 128 | + for (int i = 0; i < drones.length; i++) { |
| 129 | + int x = drones[i][0]; |
| 130 | + int y = drones[i][1]; |
| 131 | + int r = drones[i][2]; |
| 132 | + |
| 133 | + int d = Math.abs(x - tx) + Math.abs(y - ty); |
| 134 | + |
| 135 | + if (d <= r && mn > d) { |
| 136 | + ans = i; |
| 137 | + mn = d; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return ans; |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +#### C++ |
| 147 | + |
| 148 | +```cpp |
| 149 | +class Solution { |
| 150 | +public: |
| 151 | + int nearestDrone(vector<vector<int>>& drones, vector<int>& target) { |
| 152 | + int ans = -1; |
| 153 | + int mn = INT_MAX; |
| 154 | + int tx = target[0], ty = target[1]; |
| 155 | + |
| 156 | + for (int i = 0; i < drones.size(); i++) { |
| 157 | + int x = drones[i][0]; |
| 158 | + int y = drones[i][1]; |
| 159 | + int r = drones[i][2]; |
| 160 | + |
| 161 | + int d = abs(x - tx) + abs(y - ty); |
| 162 | + |
| 163 | + if (d <= r && mn > d) { |
| 164 | + ans = i; |
| 165 | + mn = d; |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + return ans; |
| 170 | + } |
| 171 | +}; |
| 172 | +``` |
| 173 | + |
| 174 | +#### Go |
| 175 | + |
| 176 | +```go |
| 177 | +func nearestDrone(drones [][]int, target []int) int { |
| 178 | + ans := -1 |
| 179 | + mn := math.MaxInt32 |
| 180 | + tx, ty := target[0], target[1] |
| 181 | + |
| 182 | + for i, drone := range drones { |
| 183 | + x, y, r := drone[0], drone[1], drone[2] |
| 184 | + |
| 185 | + d := abs(x-tx) + abs(y-ty) |
| 186 | + |
| 187 | + if d <= r && mn > d { |
| 188 | + ans = i |
| 189 | + mn = d |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + return ans |
| 194 | +} |
| 195 | + |
| 196 | +func abs(x int) int { |
| 197 | + if x < 0 { |
| 198 | + return -x |
| 199 | + } |
| 200 | + return x |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +#### TypeScript |
| 205 | + |
| 206 | +```ts |
| 207 | +function nearestDrone(drones: number[][], target: number[]): number { |
| 208 | + let ans = -1; |
| 209 | + let mn = Infinity; |
| 210 | + const [tx, ty] = target; |
| 211 | + |
| 212 | + for (let i = 0; i < drones.length; i++) { |
| 213 | + const [x, y, r] = drones[i]; |
| 214 | + |
| 215 | + const d = Math.abs(x - tx) + Math.abs(y - ty); |
| 216 | + |
| 217 | + if (d <= r && mn > d) { |
| 218 | + ans = i; |
| 219 | + mn = d; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return ans; |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +<!-- tabs:end --> |
| 228 | + |
| 229 | +<!-- solution:end --> |
| 230 | + |
| 231 | +<!-- problem:end --> |
0 commit comments