Skip to main content

Best Reachable Tower - Solution & Explanation

MediumArray11 min read
Practice this problem

Problem Statement

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].
The Manhattan Distance between two cells (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:

  • 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 <= 105
  • towers[i] = [xi, yi, qi]
  • center = [cx, cy]
  • 0 <= xi, yi, qi, cx, cy <= 105​​​​​​​
  • 0 <= radius <= 105

Approach Overview

Problem 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.

Solution

We define a variable idx to record the index of the current best tower, initially idx = -1. Then, we traverse each tower and calculate the Manhattan distance dist between it and center:

$ dist = |x_i - cx| + |y_i - cy|

If dist > radius, the tower is unreachable, so we skip it. Otherwise, we compare the quality factor q of the current tower with that of the best tower:

  • If idx = -1, it means no reachable tower has been found yet, so we update idx to the current tower's index.
  • If the current tower's quality factor q_i is greater than the best tower's quality factor q_{idx}, we update idx to the current tower's index.
  • If the current tower's quality factor q_i is equal to the best tower's quality factor q_{idx}, we compare the coordinates of the two towers and choose the one with the smaller lexicographical order.

After the traversal ends, if idx = -1, it means there are no reachable towers, so we return [-1, -1]. Otherwise, we return the coordinates of the best tower.

The time complexity is O(n), where n is the number of towers. The space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force ScanO(n^2)O(1)When first reasoning about the problem or validating the reach calculation logic
One-Pass TraversalO(n)O(1)Optimal solution for large arrays and typical interview expectations

Video Solution

Best Reachable Tower | LeetCode 3809 | Sorting • codeTips • 87 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Best Reachable Tower easy or hard?
Best Reachable Tower is typically considered a medium-level problem. The logic is simple once you recognize the greedy one-pass pattern, but identifying that a single traversal is sufficient may take some practice for beginners.
Best Reachable Tower Python/Java solution
The implementation is straightforward in Python or Java. Loop through the array, compute reach = i + towers[i], and update the maximum reach and index when a better tower is found. This produces an O(n) solution with constant additional memory.
How to solve Best Reachable Tower in O(n)?
Iterate through the array once while computing the reach of each tower as i + value. Maintain variables for the maximum reach and the corresponding tower index. Update these whenever a tower provides a larger reach. This linear scan ensures every element is processed exactly once.
What is the best approach for Best Reachable Tower?
The most efficient solution uses a one-pass traversal of the array. For each index i, compute the tower's reach using i + towers[i] and track the maximum reach seen so far. This approach processes each element once, resulting in O(n) time and O(1) space complexity.
Is Best Reachable Tower asked at Google/Amazon/Meta?
Problems based on linear array traversal and greedy reach calculations frequently appear in interviews at companies like Google, Amazon, and Meta. Variations of this pattern also appear in problems related to jump reachability and interval coverage.
What data structure is used in Best Reachable Tower?
The problem primarily uses an array and simple variables to track the maximum reach and index. No advanced data structures are required because the solution relies on a single pass and constant-time comparisons.
What is the time complexity of Best Reachable Tower?
The optimal algorithm runs in O(n) time because it scans the array once and performs constant-time calculations for each tower. The space complexity is O(1) since only a few variables are required to track the best reachable tower and its maximum reach.

Ready to solve this problem?

Practice Best Reachable Tower with our built-in code editor and test cases.

Practice on FleetCode