Skip to main content

Coordinate With Maximum Network Quality - Solution & Explanation

MediumArrayEnumeration10 min readAsked at: Lyft, Peak6
Practice this problem

Problem Statement

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.
  • ⌊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 <= 50
  • towers[i].length == 3
  • 0 <= xi, yi, qi <= 50
  • 1 <= radius <= 50

Approach Overview

Problem Overview: You receive a list of network towers where each tower has an (x, y) coordinate and a signal quality value. For any coordinate within the grid, you compute the network quality by summing contributions from towers within a given radius using the formula floor(q / (1 + distance)). The task is to return the coordinate that yields the maximum total signal quality. If multiple coordinates produce the same value, return the lexicographically smallest one.

Approach 1: Divide and Conquer Grid Evaluation (O(n * 2500) time, O(1) space)

The search space for valid coordinates is bounded because tower coordinates are within a small grid (0 to 50). You can treat the grid as a set of independent regions and evaluate signal quality for each candidate coordinate. For every point (x, y), iterate through all towers, compute the Euclidean distance, and add signal strength if the tower lies within the radius. This effectively divides the problem into many small independent computations and aggregates results to find the maximum. Despite the nested loops, the grid size is constant (51 × 51), so the approach runs efficiently in practice. The main operations are simple iteration, distance calculation, and integer flooring.

Approach 2: Dynamic Programming with Precomputed Contributions (O(n * 2500) time, O(2500) space)

This variation reduces repeated calculations by storing signal contributions for grid coordinates. For each tower, iterate over all grid points within the radius and update a 2D matrix representing accumulated network quality. The matrix acts as a dynamic programming table where dp[x][y] stores the total signal received at that coordinate so far. Each tower contributes floor(q / (1 + distance)) to nearby cells. After processing all towers, scan the table to find the coordinate with the highest signal value. This approach organizes the computation around tower updates instead of coordinate evaluation, which often improves cache locality and code clarity.

Recommended for interviews: The enumeration-based solution is what most interviewers expect. It demonstrates clear reasoning about bounded search spaces and straightforward implementation. Start with the brute-force grid scan to show correctness, then discuss how precomputing contributions with a DP-style grid reduces repeated work. Problems like this frequently appear in discussions around Array traversal and geometric Enumeration, with occasional optimization using Dynamic Programming style accumulation.

Approach 1: Divide and Conquer

This approach involves breaking the problem into smaller sub-problems, solving each sub-problem independently, and then combining the solutions to solve the original problem. The Divide and Conquer strategy is particularly useful in problems such as sorting algorithms and is often implemented via recursive methods.

The provided solution implements the Merge Sort algorithm using the divide and conquer approach in C. It uses a recursive function mergeSort to divide the array into two halves, sorts them, and then merges them using a helper function merge.

Code

C

Python

Complexity

Time Complexity: O(n log n) - The merge sort algorithm divides the array into sub-arrays, sorts and then merges them.
Space Complexity: O(n) - Additional space is used for the temporary arrays during the merge process.

Try this approach in the editor →

Approach 2: Dynamic Programming

This approach involves solving complex problems by breaking them down into simpler overlapping sub-problems that are solved independently. This technique is used when sub-problems repeat, and the results can be stored to avoid redundant calculations, optimizing time complexity. Dynamic programming can be implemented using either a top-down (memoization) or bottom-up approach.

The Java code demonstrates finding the nth Fibonacci number using a dynamic programming approach with memoization. It stores previously calculated Fibonacci values in a HashMap to prevent redundant computations, thus optimizing the time complexity.

Code

Java

JavaScript

Complexity

Time Complexity: O(n) - Each Fibonacci number is computed once and stored.
Space Complexity: O(n) - Space is used to store the computed Fibonacci values in a HashMap.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Divide and Conquer

Time Complexity: O(n log n) - The merge sort algorithm divides the array into sub-arrays, sorts and then merges them.
Space Complexity: O(n) - Additional space is used for the temporary arrays during the merge process.

Dynamic Programming

Time Complexity: O(n) - Each Fibonacci number is computed once and stored.
Space Complexity: O(n) - Space is used to store the computed Fibonacci values in a HashMap.

Default Approach

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Divide and Conquer Grid EvaluationO(n * 2500)O(1)Best when the coordinate grid is small and bounded. Simple brute-force enumeration works efficiently.
Dynamic Programming with Accumulated GridO(n * 2500)O(2500)Useful when organizing updates by tower contributions or when storing intermediate signal totals for clarity.

Video Solution

Coordinate With Maximum Network Quality Leetcode 1620 videoEasyLeetcode1,037 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Coordinate With Maximum Network Quality easy or hard?
The problem is rated Medium on LeetCode. The logic is straightforward once you recognize the grid is bounded, but many candidates initially search for complex optimizations before realizing that enumeration of all coordinates is efficient enough.
Coordinate With Maximum Network Quality Python/Java solution
Python and Java implementations both iterate through all possible grid coordinates and calculate tower contributions using Euclidean distance. Python often uses nested loops and math functions, while Java commonly uses a 2D array to store accumulated signal values.
How to solve Coordinate With Maximum Network Quality in O(n)?
A strict O(n) solution is not feasible because you must evaluate multiple candidate coordinates. However, since the coordinate range is limited, the effective complexity becomes O(n * 2500), which behaves like linear time relative to the number of towers in practice.
What is the best approach for Coordinate With Maximum Network Quality?
The most practical approach is enumerating every coordinate in the bounded grid (0–50) and computing the signal contribution from each tower. For each coordinate, calculate the Euclidean distance to towers within the radius and sum floor(q / (1 + distance)). This runs in O(n * 2500) time because the grid size is fixed and small.
Is Coordinate With Maximum Network Quality asked at Google/Amazon/Meta?
Problems involving spatial calculations, bounded grid search, and signal aggregation appear in interviews at companies like Amazon and Google. The question tests comfort with arrays, geometric distance computation, and efficient enumeration of candidate positions.
What data structure is used in Coordinate With Maximum Network Quality?
The core solution primarily uses arrays and simple iteration. Some implementations maintain a 2D grid or matrix to accumulate signal values for each coordinate, which resembles a dynamic programming table storing intermediate totals.
What is the time complexity of Coordinate With Maximum Network Quality?
The typical solution runs in O(n * 2500) time where n is the number of towers. The algorithm evaluates at most 51 × 51 grid coordinates and checks each tower to compute signal contribution. Space complexity is O(1) for direct evaluation or O(2500) if a grid table is stored.

Ready to solve this problem?

Practice Coordinate With Maximum Network Quality with our built-in code editor and test cases.

Practice on FleetCode