-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1620. Coordinate With Maximum Network Quality.cpp
- Loading branch information
1 parent
c392edc
commit b1f39b0
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//brute force | ||
//Runtime: 92 ms, faster than 48.90% of C++ online submissions for Coordinate With Maximum Network Quality. | ||
//Memory Usage: 8.9 MB, less than 7.40% of C++ online submissions for Coordinate With Maximum Network Quality. | ||
class Solution { | ||
public: | ||
double dist(int x1, int y1, int x2, int y2){ | ||
return sqrt((double)((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))); | ||
} | ||
|
||
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) { | ||
int n = towers.size(); | ||
|
||
if(n == 1 && towers[0][2] > 0){ | ||
return {towers[0][0], towers[0][1]}; | ||
} | ||
|
||
int left = INT_MAX, right = INT_MIN; | ||
int top = INT_MAX, bottom = INT_MIN; | ||
|
||
for(const vector<int>& tower : towers){ | ||
left = min(left, tower[0]); | ||
right = max(right, tower[0]); | ||
top = min(top, tower[1]); | ||
bottom = max(bottom, tower[1]); | ||
} | ||
|
||
double d; | ||
int q; | ||
int maxq = 0; | ||
vector<int> maxcoord(2, 0); | ||
for(int x = left; x <= right; ++x){ | ||
for(int y = top; y <= bottom; ++y){ | ||
q = 0; | ||
for(const vector<int>& tower : towers){ | ||
if((d = dist(x, y, tower[0], tower[1])) > radius) continue; | ||
q += tower[2]/(1+d); | ||
} | ||
if(q > maxq){ | ||
maxq = q; | ||
maxcoord[0] = x; | ||
maxcoord[1] = y; | ||
} | ||
} | ||
} | ||
|
||
return maxcoord; | ||
} | ||
}; |