[LeetCode] Coordinate With Maximum Network Quality

1620. Coordinate With Maximum Network Quality

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
radius = pow(radius, 2);
vector<int> res{0, 0, 0};
for(int i = 0; i <= 50; i++) {
for(int j = 0, sum = 0; j <= 50; j++, sum = 0) {
for(auto& t : towers) {
int d = pow(i - t[0], 2) + pow(j - t[1], 2);
if(d <= radius) sum += (t[2] / (1 + sqrt(d)));
}
if(res[2] < sum) res[0] = i, res[1] = j, res[2] = sum;
}
}
return {res[0], res[1]};
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/01/16/PS/LeetCode/coordinate-with-maximum-network-quality/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.