[LeetCode] Nearest Available Drone

4024. Nearest Available Drone

You are given a 2D integer array drones, where drones[i] = [x_i, y_i, range_i] represents the x-coordinate, y-coordinate, and travel range of the i^th drone.

You are also given an integer array target = [tx, ty], representing the coordinates of the target.

A drone drones[i] can reach the target if the Manhattan distance between its coordinates and the target coordinates is less than or equal to its range_i.

Return the index of the reachable drone with the minimum Manhattan distance to the target. If there is a tie, return the smallest index. If no drone can reach the target, return -1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int nearestDrone(vector<vector<int>>& drones, vector<int>& target) {
int res = -1, best = INT_MAX;
for(int i = 0; i < drones.size(); i++) {
int dist = abs(drones[i][0] - target[0]) + abs(drones[i][1] - target[1]);
if(dist <= drones[i][2] and dist < best) {
best = dist;
res = i;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/03/PS/LeetCode/nearest-available-drone/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.