Skip to content

Commit 7febb87

Browse files
authored
Create coordinate-with-maximum-network-quality.cpp
1 parent 2adaba0 commit 7febb87

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Time: O(n^2)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
7+
int min_x = numeric_limits<int>::max();
8+
int max_x = numeric_limits<int>::min();
9+
int min_y = numeric_limits<int>::max();
10+
int max_y = numeric_limits<int>::min();
11+
for (const auto& tower: towers) {
12+
min_x = min(min_x, tower[0]);
13+
max_x = max(max_x, tower[0]);
14+
min_y = min(min_y, tower[1]);
15+
max_y = max(max_y, tower[1]);
16+
}
17+
int max_quality = 0;
18+
vector<int> result;
19+
for (int x = min_x; x <= max_x; ++x) {
20+
for (int y = min_y; y <= max_y; ++y) {
21+
int q = 0;
22+
for (const auto& t : towers) {
23+
double d = sqrt((t[0] - x) * (t[0] - x) + (t[1] - y) * (t[1] - y));
24+
if (d <= radius) {
25+
q += int(t[2] / (1 + d));
26+
}
27+
}
28+
if (q > max_quality) {
29+
max_quality = q;
30+
result = {x, y};
31+
}
32+
}
33+
}
34+
return result;
35+
}
36+
};

0 commit comments

Comments
 (0)