Skip to content

Commit

Permalink
priority_queue, slower
Browse files Browse the repository at this point in the history
  • Loading branch information
keineahnung2345 authored Oct 25, 2020
1 parent b1f39b0 commit f96feb8
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions 1620. Coordinate With Maximum Network Quality.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,48 @@ class Solution {
return maxcoord;
}
};

//priority_queue, slower
//https://leetcode.com/problems/coordinate-with-maximum-network-quality/discuss/898702/Try-all-x-y-coordinates-in-range-or-Detailed-Explanation
//Runtime: 536 ms, faster than 18.23% of C++ online submissions for Coordinate With Maximum Network Quality.
//Memory Usage: 39.8 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();

double d;
int q;
int maxq = 0;
vector<int> maxcoord(2, 0);

//(quality, x, y)
//the larger quality, smaller x and smaller y will be popped earlier
auto comp = [](vector<int>& a, vector<int>& b){
return (a[0]==b[0]) ? ((a[1]==b[1]) ? (a[2] > b[2]) : (a[1] > b[1])) : (a[0] < b[0]);
};
priority_queue<vector<int>, vector<vector<int>>, decltype(comp)> pq(comp);

for(int x = 0; x <= 50; ++x){
for(int y = 0; y <= 50; ++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);
}
pq.push({q, x, y});
}
}

// while(!pq.empty()){
// cout << pq.top()[0] << ", " << pq.top()[1] << ", " << pq.top()[2] << endl;
// pq.pop();
// }

return {pq.top()[1], pq.top()[2]};
}
};

0 comments on commit f96feb8

Please sign in to comment.