Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 to radius. 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 the integral coordinate (cx, cy) where the network quality is maximum. If there are multiple coordinates with the same network quality, return the lexicographically minimum non-negative coordinate.
Note:
(x1, y1) is lexicographically smaller than (x2, y2) if either:
x1 < x2, orx1 == x2 and y1 < y2.⌊val⌋ is the greatest integer less than or equal to val (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 <= xi, yi, qi <= 501 <= radius <= 50Problem summary: 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 to radius. 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 the integral coordinate (cx, cy) where the network quality is maximum. If there are multiple
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,2,5],[2,1,7],[3,1,9]] 2
[[23,11,21]] 9
[[1,2,13],[2,1,7],[0,1,9]] 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1620: Coordinate With Maximum Network Quality
class Solution {
public int[] bestCoordinate(int[][] towers, int radius) {
int mx = 0;
int[] ans = new int[] {0, 0};
for (int i = 0; i < 51; ++i) {
for (int j = 0; j < 51; ++j) {
int t = 0;
for (var e : towers) {
double d = Math.sqrt((i - e[0]) * (i - e[0]) + (j - e[1]) * (j - e[1]));
if (d <= radius) {
t += Math.floor(e[2] / (1 + d));
}
}
if (mx < t) {
mx = t;
ans = new int[] {i, j};
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #1620: Coordinate With Maximum Network Quality
func bestCoordinate(towers [][]int, radius int) []int {
ans := []int{0, 0}
mx := 0
for i := 0; i < 51; i++ {
for j := 0; j < 51; j++ {
t := 0
for _, e := range towers {
d := math.Sqrt(float64((i-e[0])*(i-e[0]) + (j-e[1])*(j-e[1])))
if d <= float64(radius) {
t += int(float64(e[2]) / (1 + d))
}
}
if mx < t {
mx = t
ans = []int{i, j}
}
}
}
return ans
}
# Accepted solution for LeetCode #1620: Coordinate With Maximum Network Quality
class Solution:
def bestCoordinate(self, towers: List[List[int]], radius: int) -> List[int]:
mx = 0
ans = [0, 0]
for i in range(51):
for j in range(51):
t = 0
for x, y, q in towers:
d = ((x - i) ** 2 + (y - j) ** 2) ** 0.5
if d <= radius:
t += floor(q / (1 + d))
if t > mx:
mx = t
ans = [i, j]
return ans
// Accepted solution for LeetCode #1620: Coordinate With Maximum Network Quality
struct Solution;
use std::cmp::Reverse;
impl Solution {
fn best_coordinate(towers: Vec<Vec<i32>>, radius: i32) -> Vec<i32> {
let mut max = (0, Reverse(0), Reverse(0));
for xi in 0..=50 {
for yi in 0..=50 {
let mut sum = 0;
for point in towers.iter() {
let x = point[0];
let y = point[1];
let q = point[2];
if (x - xi) * (x - xi) + (y - yi) * (y - yi) > radius * radius {
continue;
}
let d = (((x - xi) * (x - xi) + (y - yi) * (y - yi)) as f64).sqrt();
sum += (q as f64 / (1.0 + d)).floor() as i32;
}
if (sum, Reverse(xi), Reverse(yi)) > max {
max = (sum, Reverse(xi), Reverse(yi));
}
}
}
vec![(max.1).0, (max.2).0]
}
}
#[test]
fn test() {
let towers = vec_vec_i32![[1, 2, 5], [2, 1, 7], [3, 1, 9]];
let radius = 2;
let res = vec![2, 1];
assert_eq!(Solution::best_coordinate(towers, radius), res);
let towers = vec_vec_i32![[23, 11, 21]];
let radius = 9;
let res = vec![23, 11];
assert_eq!(Solution::best_coordinate(towers, radius), res);
let towers = vec_vec_i32![[1, 2, 13], [2, 1, 7], [0, 1, 9]];
let radius = 2;
let res = vec![1, 2];
assert_eq!(Solution::best_coordinate(towers, radius), res);
let towers = vec_vec_i32![[2, 1, 9], [0, 1, 9]];
let radius = 2;
let res = vec![0, 1];
assert_eq!(Solution::best_coordinate(towers, radius), res);
}
// Accepted solution for LeetCode #1620: Coordinate With Maximum Network Quality
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1620: Coordinate With Maximum Network Quality
// class Solution {
// public int[] bestCoordinate(int[][] towers, int radius) {
// int mx = 0;
// int[] ans = new int[] {0, 0};
// for (int i = 0; i < 51; ++i) {
// for (int j = 0; j < 51; ++j) {
// int t = 0;
// for (var e : towers) {
// double d = Math.sqrt((i - e[0]) * (i - e[0]) + (j - e[1]) * (j - e[1]));
// if (d <= radius) {
// t += Math.floor(e[2] / (1 + d));
// }
// }
// if (mx < t) {
// mx = t;
// ans = new int[] {i, j};
// }
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.