Let's make this one less mysterious. For 1620. Coordinate With Maximum Network Quality, the solution in this repository is mainly a heap / priority queue solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: heap / priority queue, two pointers, sliding window.
The notes already sitting in the source point us in the right direction:
- brute force
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are dist, bestCoordinate, maxcoord.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The queue gives the solution a level-by-level or frontier-style traversal.
- The heap keeps the best candidate available without sorting the whole world every time.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//brute force
02//Runtime: 92 ms, faster than 48.90% of C++ online submissions for Coordinate With Maximum Network Quality.
03//Memory Usage: 8.9 MB, less than 7.40% of C++ online submissions for Coordinate With Maximum Network Quality.
04class Solution {
05public:
06 double dist(int x1, int y1, int x2, int y2){
07 return sqrt((double)((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)));
08 }
09
10 vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
11 int n = towers.size();
12
13 if(n == 1 && towers[0][2] > 0){
14 return {towers[0][0], towers[0][1]};
15 }
16
17 int left = INT_MAX, right = INT_MIN;
18 int top = INT_MAX, bottom = INT_MIN;
19
20 for(const vector<int>& tower : towers){
21 left = min(left, tower[0]);
22 right = max(right, tower[0]);
23 top = min(top, tower[1]);
24 bottom = max(bottom, tower[1]);
25 }
26
27 double d;
28 int q;
29 int maxq = 0;
30 vector<int> maxcoord(2, 0);
31 for(int x = left; x <= right; ++x){
32 for(int y = top; y <= bottom; ++y){
33 q = 0;
34 for(const vector<int>& tower : towers){
35 if((d = dist(x, y, tower[0], tower[1])) > radius) continue;
36 q += tower[2]/(1+d);
37 }
38 if(q > maxq){
39 maxq = q;
40 maxcoord[0] = x;
41 maxcoord[1] = y;
42 }
43 }
44 }
45
46 return maxcoord;
47 }
48};
49
50//priority_queue, slower
51//https://leetcode.com/problems/coordinate-with-maximum-network-quality/discuss/898702/Try-all-x-y-coordinates-in-range-or-Detailed-Explanation
52//Runtime: 536 ms, faster than 18.23% of C++ online submissions for Coordinate With Maximum Network Quality.
53//Memory Usage: 39.8 MB, less than 7.40% of C++ online submissions for Coordinate With Maximum Network Quality.
54class Solution {
55public:
56 double dist(int x1, int y1, int x2, int y2){
57 return sqrt((double)((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)));
58 }
59
60 vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
61 int n = towers.size();
62
63 double d;
64 int q;
65 int maxq = 0;
66 vector<int> maxcoord(2, 0);
67
68 //(quality, x, y)
69 //the larger quality, smaller x and smaller y will be popped earlier
70 auto comp = [](vector<int>& a, vector<int>& b){
71 return (a[0]==b[0]) ? ((a[1]==b[1]) ? (a[2] > b[2]) : (a[1] > b[1])) : (a[0] < b[0]);
72 };
73 priority_queue<vector<int>, vector<vector<int>>, decltype(comp)> pq(comp);
74
75 for(int x = 0; x <= 50; ++x){
76 for(int y = 0; y <= 50; ++y){
77 q = 0;
78 for(const vector<int>& tower : towers){
79 if((d = dist(x, y, tower[0], tower[1])) > radius) continue;
80 q += tower[2]/(1+d);
81 }
82 pq.push({q, x, y});
83 }
84 }
85
86 // while(!pq.empty()){
87 // cout << pq.top()[0] << ", " << pq.top()[1] << ", " << pq.top()[2] << endl;
88 // pq.pop();
89 // }
90
91 return {pq.top()[1], pq.top()[2]};
92 }
93};
Cost