Coordinate With Maximum Network Quality Problem
Description
LeetCode Problem 1620.
You are given an array of network towers towers, where towers[i] = [x_i, y_i, q_i] denotes the i^th network tower with location (x_i, y_i) and quality factor q_i. All the coordinates are integral coordinates on the X-Y plane, and the distance between the two coordinates is the Euclidean distance.
You are also given an integer radius where a tower is reachable if the distance is less than or equal to radius. Outside that distance, the signal becomes garbled, and the tower is not reachable.
The signal quality of the i^th tower at a coordinate (x, y) is calculated with the formula ⌊q_i / (1 + d)⌋, where d is the distance between the tower and the coordinate. The network quality at a coordinate is the sum of the signal qualities from all the reachable towers.
Return the array [c_x, c_y] representing the integral coordinate (c_x, c_y) where the network quality is maximum. If there are multiple coordinates with the same network quality, return the lexicographically minimum non-negative coordinate.
Note:
- A coordinate
(x1, y1)is lexicographically smaller than(x2, y2)if either:x1 < x2, orx1 == x2andy1 < y2.
⌊val⌋is the greatest integer less than or equal toval(the floor function).
Example 1:

Input: towers = [[1,2,5],[2,1,7],[3,1,9]], radius = 2
Output: [2,1]
Explanation: At coordinate (2, 1) the total quality is 13.
- Quality of 7 from (2, 1) results in ⌊7 / (1 + sqrt(0)⌋ = ⌊7⌋ = 7
- Quality of 5 from (1, 2) results in ⌊5 / (1 + sqrt(2)⌋ = ⌊2.07⌋ = 2
- Quality of 9 from (3, 1) results in ⌊9 / (1 + sqrt(1)⌋ = ⌊4.5⌋ = 4
No other coordinate has a higher network quality.
Example 2:
Input: towers = [[23,11,21]], radius = 9
Output: [23,11]
Explanation: Since there is only one tower, the network quality is highest right at the tower's location.
Example 3:
Input: towers = [[1,2,13],[2,1,7],[0,1,9]], radius = 2
Output: [1,2]
Explanation: Coordinate (1, 2) has the highest network quality.
Constraints:
1 <= towers.length <= 50towers[i].length == 30 <= x_i, y_i, q_i <= 501 <= radius <= 50
Solution
Approach: Try Every Point
All tower coordinates are between 0 and 50. Moving away from that square only takes you farther from every tower, so the best point is also inside it, and there are just 51 × 51 points to try.
For each point (x, y), add up the signal of every tower:
- Compare the squared distance
dx * dx + dy * dywithradius * radius. This keeps the reachability test in integers, so a tower exactly at distanceradiusis never lost to rounding. - For a reachable tower, add
floor(q / (1 + d))withd = sqrt(dx * dx + dy * dy).
Walk the points in lexicographic order (x first, then y) and replace the best point only when the quality is strictly greater. Then, on a tie, the earliest point, which is the lexicographically smallest, stays.
Start with quality 0 and point [0, 0]: if no point gets any signal, the answer is [0, 0], the smallest non-negative coordinate.
Complexity: O(512 · t) time for t towers, and O(1) extra space.
import math
class Solution:
def bestCoordinate(self, towers: List[List[int]], radius: int) -> List[int]:
best, ans = 0, [0, 0]
# Every tower is inside [0, 50] x [0, 50], so the best point is there too.
for x in range(51):
for y in range(51):
quality = 0
for tx, ty, q in towers:
sq = (tx - x) ** 2 + (ty - y) ** 2
if sq <= radius * radius:
quality += int(q / (1 + math.sqrt(sq)))
# Strictly greater: on a tie the earlier (smaller) coordinate stays.
if quality > best:
best, ans = quality, [x, y]
return ans