Best Reachable Tower Problem
Description
LeetCode Problem 3809.
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].
The Manhattan Distance between two cells (x_i, y_i) and (x_j, y_j) is |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.
Example 1:
Input: towers = [[1,2,5], [2,1,7], [3,1,9]], center = [1,1], radius = 2
Output: [3,1]
Explanation:
- Tower [1, 2, 5]: Manhattan distance = |1 - 1| + |2 - 1| = 1, reachable.
- Tower [2, 1, 7]: Manhattan distance = |2 - 1| + |1 - 1| = 1, reachable.
- Tower [3, 1, 9]: Manhattan distance = |3 - 1| + |1 - 1| = 2, reachable.
All towers are reachable. The maximum quality factor is 9, which corresponds to tower [3, 1].
Example 2:
Input: towers = [[1,3,4], [2,2,4], [4,4,7]], center = [0,0], radius = 5
Output: [1,3]
Explanation:
- Tower [1, 3, 4]: Manhattan distance = |1 - 0| + |3 - 0| = 4, reachable.
- Tower [2, 2, 4]: Manhattan distance = |2 - 0| + |2 - 0| = 4, reachable.
- Tower [4, 4, 7]: Manhattan distance = |4 - 0| + |4 - 0| = 8, not reachable.
Among the reachable towers, the maximum quality factor is 4. Both [1, 3] and [2, 2] have the same quality, so the lexicographically smaller coordinate is [1, 3].
Example 3:
Input: towers = [[5,6,8], [0,3,5]], center = [1,2], radius = 1
Output: [-1,-1]
Explanation:
- Tower [5, 6, 8]: Manhattan distance = |5 - 1| + |6 - 2| = 8, not reachable.
- Tower [0, 3, 5]: Manhattan distance = |0 - 1| + |3 - 2| = 2, not reachable.
No tower is reachable within the given radius, so [-1, -1] is returned.
Constraints:
1 <= towers.length <= 10^5towers[i] = [x_i, y_i, q_i]center = [cx, cy]0 <= x_i, y_i, q_i, cx, cy <= 10^50 <= radius <= 10^5
Solution
Approach: One Pass
Walk through the towers and keep the index idx of the best reachable one so far (start with -1). Skip a tower if its Manhattan distance to center is more than radius. Otherwise it replaces the current best when there is none yet, when its quality is higher, or when the quality is equal and its coordinates are lexicographically smaller.
At the end return the best tower's coordinates, or [-1, -1] if there was none.
Complexity: Time O(n), space O(1), where n is the number of towers.
class Solution:
def bestTower(
self, towers: List[List[int]], center: List[int], radius: int
) -> List[int]:
cx, cy = center
idx = -1
for i, (x, y, q) in enumerate(towers):
dist = abs(x - cx) + abs(y - cy)
if dist > radius:
continue
if (
idx == -1
or towers[idx][2] < q
or (towers[idx][2] == q and towers[i][:2] < towers[idx][:2])
):
idx = i
return [-1, -1] if idx == -1 else towers[idx][:2]