#3809
Medium Algorithms Best reachable tower
Array
55.7% acceptance
Mar 16, 2026
57
6
You are given a 2D integer array towers, where towers[i] = [xi, yi, qi] represents the coordinates (xi, yi) and quality factor qi of the ith 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].
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn best_tower(towers: Vec<Vec<i32>>, center: Vec<i32>, radius: i32) -> Vec<i32> {
let cx = center[0];
let cy = center[1];
let mut best_q = -1;
let mut best_coord = vec![-1, -1];
for t in &towers {
let dist = (t[0] - cx).abs() + (t[1] - cy).abs();
if dist <= radius {
let q = t[2];
if q > best_q || (q == best_q && (t[0] < best_coord[0] || (t[0] == best_coord[0] && t[1] < best_coord[1]))) {
best_q = q;
best_coord = vec![t[0], t[1]];
}
}
}
best_coord
}
}