↓Skip to main content
  1. LeetCode/

LeetCode 3809: Best Reachable Tower

·1 min· ·
LeetCode Medium Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-17

  • Submissions: Accepted (first try)

Approach
#

“Highest quality, ties to the lexicographically smallest coordinate” merges into one key: the minimum of (-q, x, y) (Array Template 4). Consider only towers with Manhattan distance |x - cx| + |y - cy| <= radius, and return [-1, -1] if none.

My Solution
#

class Solution:
    def bestTower(self, towers: List[List[int]], center: List[int], radius: int) -> List[int]:
        cx, cy = center
        reachable = [(-q, x, y) for x, y, q in towers if abs(x - cx) + abs(y - cy) <= radius]
        if not reachable:
            return [-1, -1]
        _, x, y = min(reachable)
        return [x, y]

I originally compared quality, x and y in an if / elif chain; a tuple key is harder to get wrong.

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Quality should be maximal, so the key uses -q to take the minimum together with the coordinates.

Interview Explanation
#

I keep towers within the radius and take the minimum of (negative quality, x, y), which picks the highest quality with the smallest coordinate on ties; with none, return [-1, -1]. Time O(n).

Related

LeetCode 3810: Minimum Operations to Reach Target Array
·2 mins
LeetCode Medium Greedy Hash-Table Array
LeetCode 3810 note: one operation fixes every position holding a value, so the answer is the number of distinct values at mismatched positions
LeetCode 2555: Maximize Win From Two Segments
·2 mins
LeetCode Medium Sliding-Window Binary-Search Array
LeetCode 2555 note: a sliding window gives how many prizes a segment ending at each prize collects, and a running “best single segment among the first i prizes” supplies the second segment to its left
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 3804: Number of Centered Subarrays
·1 min
LeetCode Medium Hash-Table Enumeration Array
LeetCode 3804 note: fix the left end and extend right, adding to the sum and to a set; count the subarray when the sum is in the set
LeetCode 2958: Length of Longest Subarray With at Most K Frequency
·1 min
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2958 note: a longest-window slide that shrinks the left whenever the newly added number appears more than k times