[LeetCode] Best Reachable Tower

3809. Best Reachable Tower

You are given a 2D integer array towers, where towers[i] = [x_i, y_i, q_i] represents the coordinates (x_i, y_i) and quality factor q_i of the i^th tower.

You are also given an integer array center = [cx, cy​​​​​​​] representing your location, and an integer radius.

A tower is reachable if its Manhattan distance from center is less than or equal to radius.

Among all reachable towers:

  • Return the coordinates of the tower with the maximum quality factor.
  • If there is a tie, return the tower with the lexicographically smallest coordinate. If no tower is reachable, return [-1, -1].

Manhattan Distance

(x_i, y_i)

(x_j, y_j)

|x_i - x_j| + |y_i - y_j|

A coordinate [x_i, y_i] is lexicographically smaller than [x_j, y_j] if x_i < x_j, or x_i == x_j and y_i < y_j.

|x| denotes the absolute value of x.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

class Solution {
public:
vector<int> bestTower(vector<vector<int>>& towers, vector<int>& center, int radius) {
int best = -1;
auto ok = [&](int idx) {
int d = abs(towers[idx][0] - center[0]) + abs(towers[idx][1] - center[1]);
return d <= radius;
};
for(int i = 0; i < towers.size(); i++) {
if(!ok(i)) continue;
if(best == -1) best = i;
else if(towers[best][2] < towers[i][2]) best = i;
else if(towers[best][2] == towers[i][2] and towers[best] > towers[i]) best = i;
}
if(best == -1) return {-1,-1};
return {towers[best][0],towers[best][1]};
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/best-reachable-tower/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.