You are given an array of network towers towers, where towers[i] = [xi, yi, qi] denotes the ith network tower with location (xi, yi) and quality factor qi. 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 toradius. Outside that distance, the signal becomes garbled, and the tower is not reachable.
The signal quality of the ith tower at a coordinate (x, y) is calculated with the formula ⌊qi / (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[cx, cy]_representing theintegral coordinate _(cx, cy)where thenetwork 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, or
x1 == x2 and y1 < y2.
⌊val⌋ is the greatest integer less than or equal to val (the floor function).

Input: towers =[[1,2,5],[2,1,7],[3,1,9]], radius =2Output: [2,1]Explanation: At coordinate(2,1) the total quality is13.- Quality of 7from(2,1) results in⌊7/(1+ sqrt(0)⌋=⌊7⌋=7- Quality of 5from(1,2) results in⌊5/(1+ sqrt(2)⌋=⌊2.07⌋=2- Quality of 9from(3,1) results in⌊9/(1+ sqrt(1)⌋=⌊4.5⌋=4No other coordinate has a higher network quality.
Input: towers =[[23,11,21]], radius =9Output: [23,11]Explanation: Since there is only one tower, the network quality is highest right at the tower's location.
The best coordinate must be an integer point within the bounding box of all towers, since the signal quality only changes at integer coordinates. We can check all integer points in this range and compute the total network quality for each, picking the one with the highest quality (and lexicographically smallest in case of ties).
classSolution {
funbestCoordinate(towers: Array<IntArray>, radius: Int): IntArray {
var minX = towers[0][0]
var minY = towers[0][1]
var maxX = towers[0][0]
var maxY = towers[0][1]
for (t in towers) {
minX = minOf(minX, t[0])
minY = minOf(minY, t[1])
maxX = maxOf(maxX, t[0])
maxY = maxOf(maxY, t[1])
}
var ans = intArrayOf(0, 0)
var maxQ = 0for (x in minX..maxX) {
for (y in minY..maxY) {
var q = 0for (t in towers) {
val dx = t[0] - x
val dy = t[1] - y
val d = Math.sqrt((dx * dx + dy * dy).toDouble())
if (d <= radius) q +=Math.floor(t[2] / (1 + d)).toInt()
}
if (q > maxQ || (q == maxQ && (x < ans[0] || (x == ans[0] && y < ans[1])))) {
maxQ = q
ans = intArrayOf(x, y)
}
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution:
defbestCoordinate(self, towers: list[list[int]], radius: int) -> list[int]:
min_x = min(t[0] for t in towers)
max_x = max(t[0] for t in towers)
min_y = min(t[1] for t in towers)
max_y = max(t[1] for t in towers)
ans = [0, 0]
max_q =0for x in range(min_x, max_x +1):
for y in range(min_y, max_y +1):
q =0for tx, ty, tq in towers:
d = ((tx - x) **2+ (ty - y) **2) **0.5if d <= radius:
q += int(tq // (1+ d))
if q > max_q or (q == max_q and (x < ans[0] or (x == ans[0] and y < ans[1]))):
max_q = q
ans = [x, y]
return ans
⏰ Time complexity: O(n * W * H), where n is the number of towers, and W, H are the width and height of the bounding box. For each integer coordinate, we check all towers.
🧺 Space complexity: O(1), as only a constant amount of space is used for tracking the answer.