Coordinate With Maximum Network Quality — LeetCode 1620 Python Solution

MediumArrayEnumeration
Problem
#1620
Reading time
3 min

The 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.

Example

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.

Python solution

Python
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

Complexity

MeasureComplexity
TimeO(n)
SpaceO(1) to O(n) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1620. Coordinate With Maximum Network Quality?
LeetCode 1620. Coordinate With Maximum Network Quality is rated Medium on LeetCode.
What topics does LeetCode 1620. Coordinate With Maximum Network Quality cover?
LeetCode 1620. Coordinate With Maximum Network Quality is tagged Array and Enumeration on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview