diff --git a/1620. Coordinate With Maximum Network Quality.cpp b/1620. Coordinate With Maximum Network Quality.cpp index ed8a3c8..bdc37ff 100644 --- a/1620. Coordinate With Maximum Network Quality.cpp +++ b/1620. Coordinate With Maximum Network Quality.cpp @@ -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 bestCoordinate(vector>& towers, int radius) { + int n = towers.size(); + + double d; + int q; + int maxq = 0; + vector maxcoord(2, 0); + + //(quality, x, y) + //the larger quality, smaller x and smaller y will be popped earlier + auto comp = [](vector& a, vector& 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>, decltype(comp)> pq(comp); + + for(int x = 0; x <= 50; ++x){ + for(int y = 0; y <= 50; ++y){ + q = 0; + for(const vector& 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]}; + } +};