Watch 3 video solutions for Best Reachable Tower, a medium level problem involving Array. This walkthrough by codeTips has 80 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
[-1, -1].(xi, yi) and (xj, yj) is |xi - xj| + |yi - yj|.
A coordinate [xi, yi] is lexicographically smaller than [xj, yj] if xi < xj, or xi == xj and yi < yj.
|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:
[1, 2, 5]: Manhattan distance = |1 - 1| + |2 - 1| = 1, reachable.[2, 1, 7]: Manhattan distance = |2 - 1| + |1 - 1| = 1, reachable.[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:
[1, 3, 4]: Manhattan distance = |1 - 0| + |3 - 0| = 4, reachable.[2, 2, 4]: Manhattan distance = |2 - 0| + |2 - 0| = 4, reachable.[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:
[5, 6, 8]: Manhattan distance = |5 - 1| + |6 - 2| = 8, not reachable.[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 <= 105towers[i] = [xi, yi, qi]center = [cx, cy]0 <= xi, yi, qi, cx, cy <= 1050 <= radius <= 105Problem Overview: You are given an array where each index represents a tower and the value describes how far that tower can reach. The goal is to determine which tower provides the best reach — the one that can extend to the farthest index in the array.
Approach 1: Brute Force Scan (O(n^2) time, O(1) space)
The straightforward idea is to evaluate the reach of every tower and compare it with all other towers. For each index i, compute the farthest position it can reach using its value and check whether that reach beats the best one seen so far. If additional constraints require verifying reachable ranges explicitly, you might scan the covered range for each tower. This nested iteration leads to O(n^2) time in the worst case and constant extra space. It demonstrates the core observation but becomes inefficient for large arrays.
Approach 2: One-Pass Traversal (O(n) time, O(1) space)
The optimal approach relies on a single pass through the array. While iterating from left to right, compute each tower's effective reach using reach = i + towers[i]. Track the maximum reach encountered and the tower index responsible for it. Because every tower is processed once and the best candidate is updated in constant time, the algorithm runs in O(n) time with O(1) extra space. This works because the problem only requires comparing each tower's reach, which can be computed independently without revisiting earlier elements.
This technique is a classic linear scan over an array, where the algorithm maintains a running best value. Similar patterns appear in greedy traversal problems such as maximum reach or jump coverage. The key insight is that the reach of each tower is determined locally, so a single traversal is enough to determine the global optimum without extra data structures.
Recommended for interviews: The one-pass traversal approach. Interviewers expect you to recognize that each tower's reach can be computed independently and compared during a single iteration. Explaining the brute force idea first shows you understand the problem space, but implementing the O(n) scan demonstrates strong command of array traversal and greedy evaluation patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Scan | O(n^2) | O(1) | When first reasoning about the problem or validating the reach calculation logic |
| One-Pass Traversal | O(n) | O(1) | Optimal solution for large arrays and typical interview expectations |