Coordinate With Maximum Network Quality - Problem

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: A coordinate (x1, y1) is lexicographically smaller than (x2, y2) if either: x1 < x2, or x1 == x2 and y1 < y2.

Input & Output

Example 1 — Basic Case
$ Input: towers = [[1,2,5],[2,1,7],[3,1,9]], radius = 2
Output: [2,1]
💡 Note: At coordinate (2,1): Tower1 distance=√2≈1.41, quality=⌊5/(1+1.41)⌋=2. Tower2 distance=0, quality=⌊7/(1+0)⌋=7. Tower3 distance=1, quality=⌊9/(1+1)⌋=4. Total=2+7+4=13, which is maximum.
Example 2 — Multiple Optimal Points
$ Input: towers = [[23,11,21]], radius = 9
Output: [23,11]
💡 Note: Only one tower, so the coordinate at the tower location (23,11) gives maximum quality: ⌊21/(1+0)⌋=21.
Example 3 — No Signal Case
$ Input: towers = [[1,2,13],[2,1,7],[0,1,9]], radius = 0
Output: [1,2]
💡 Note: With radius=0, only coordinates exactly at tower locations get signal. Tower at (1,2) has highest quality 13, so return [1,2].

Constraints

  • 1 ≤ towers.length ≤ 50
  • towers[i].length == 3
  • 0 ≤ xi, yi, qi ≤ 50
  • 1 ≤ radius ≤ 50

Visualization

Tap to expand
Coordinate With Maximum Network Quality INPUT 1 2 3 1 2 T1 q=5 T2 q=7 T3 q=9 towers = [ [1,2,5], [2,1,7], [3,1,9] ] radius = 2 [x, y, quality] ALGORITHM STEPS 1 Define Search Area Find min/max coords from towers x: [1,3], y: [1,2] 2 Check Each Coordinate Iterate all (x,y) in range Quality Calculation at (2,1) T1: d=1.41, q=5/(1+1.41)=2 T2: d=0, q=7/(1+0)=7 T3: d=1, q=9/(1+1)=4 Total = 2+7+4 = 13 3 Track Maximum Keep best coord with max quality 4 Lexicographic Tie-break Smaller (x,y) wins if equal FINAL RESULT 1 2 3 1 2 Output: [2, 1] OK - Maximum Quality: 13 Lexicographically smallest Key Insight: Instead of checking all possible coordinates, focus on the vicinity of towers (within radius). Signal quality formula: floor(q / (1 + distance)). Only towers within radius contribute. Time: O(n * W * H) where W,H are search dimensions | Space: O(1) TutorialsPoint - Coordinate With Maximum Network Quality | Optimized Search - Focus on Tower Vicinity
Asked in
Google 12 Amazon 8 Apple 6
8.9K Views
Medium Frequency
~25 min Avg. Time
245 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen