Advertisement

Coordinate With Maximum Network Quality - LeetCode 1620 Solution

Coordinate With Maximum Network Quality - Complete Solution Guide

Coordinate With Maximum Network Quality is LeetCode problem 1620, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 reachabl

Detailed Explanation

The problem asks us to find the coordinate (cx, cy) on the X-Y plane that maximizes the network quality, given a list of network towers with their locations (xi, yi) and quality factors qi, and a radius. The network quality at a coordinate (x, y) is the sum of the signal qualities from all reachable towers. A tower is reachable if its distance to the coordinate is less than or equal to the given radius. The signal quality of a tower at a coordinate is calculated as floor(qi / (1 + d)), where d is the Euclidean distance between the tower and the coordinate. If multiple coordinates have the same maximum network quality, we must return the lexicographically smallest non-negative coordinate. The constraints specify the ranges for the tower counts, coordinates, quality factors, and the radius.

Solution Approach

The solution iterates through all possible integral coordinates (cx, cy) from (0, 0) to (50, 50). For each coordinate, it calculates the network quality by iterating through all the towers. For each tower, it calculates the squared distance to the current coordinate. If the squared distance is within the squared radius, the signal quality is calculated and added to the total network quality for that coordinate. After calculating the network quality for each coordinate, the solution compares it to the current maximum quality and updates the best coordinate if the current quality is higher or if it's the same but lexicographically smaller.

Step-by-Step Algorithm

  1. Step 1: Initialize `max_quality` to -1 and `best_coord` to [0, 0].
  2. Step 2: Iterate through all possible coordinates (cx, cy) where cx ranges from 0 to 50 and cy ranges from 0 to 50.
  3. Step 3: For each coordinate (cx, cy), initialize `current_quality` to 0.
  4. Step 4: Iterate through all towers.
  5. Step 5: For each tower (tx, ty, q), calculate the squared Euclidean distance d_sq between (cx, cy) and (tx, ty).
  6. Step 6: If d_sq is less than or equal to the squared radius, calculate the signal quality signal = floor(q / (1 + sqrt(d_sq))).
  7. Step 7: Add signal to `current_quality`.
  8. Step 8: After iterating through all towers, compare `current_quality` with `max_quality`.
  9. Step 9: If `current_quality` is greater than `max_quality`, update `max_quality` to `current_quality` and `best_coord` to [cx, cy].
  10. Step 10: After iterating through all possible coordinates, return `best_coord`.

Key Insights

  • Insight 1: The problem is essentially a brute-force search within a limited coordinate space (0 to 50 for both x and y). Since the constraints are small (maximum coordinate value is 50), iterating through all possible coordinates is feasible.
  • Insight 2: We need to efficiently calculate the Euclidean distance between a candidate coordinate and each tower, and determine if the tower is within the given radius.
  • Insight 3: Remember to use the floor function when calculating the signal quality. Handle lexicographical ordering correctly when updating the best coordinate.

Complexity Analysis

Time Complexity: O(N)

Space Complexity: O(1)

Topics

This problem involves: Array, Enumeration.

Companies

Asked at: Lyft, peak6.