The trick here is to name the state correctly, then let the implementation follow. For 149. Max Points on a Line, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. 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: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- {slope, intercept} counter
- WA
- https://leetcode.com/submissions/detail/379863182/
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are maxPoints, add_line, max_count_on_lines.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- A map keeps the lookup side cheap; the code pays a little memory to avoid repeated searching.
- A set is doing the membership or uniqueness work, which keeps the main loop readable.
- 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:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
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//{slope, intercept} counter
02//WA
03//https://leetcode.com/submissions/detail/379863182/
04class Solution {
05public:
06 int maxPoints(vector<vector<int>>& points) {
07 unordered_map<double, int> slope_map, intc_map;
08 // unordered_map<pair<double, double>, int> lineCounter;
09 map<pair<double, double>, unordered_set<int>> lineCounter;
10 // map<pair<int, int>, int> lineCounter;
11
12 int n = points.size();
13 if(n == 1) return 1;
14
15 double slope, intc;
16 int slope_idx, intc_idx;
17 int ans = 0;
18
19 for(int i = 0; i < n; ++i){
20 for(int j = i+1; j < n; ++j){
21 // cout << "(" << i << ", " << j << "): " << endl;
22 if(points[i][0] == points[j][0]){
23 slope = INT_MAX;
24 intc = points[i][0];
25 }else{
26 // cout << "diffy: " << points[i][1] - points[j][1] << ", diffx: " << points[i][0] - points[j][0] << endl;
27 slope = (double)(points[i][1] - points[j][1])/(points[i][0] - points[j][0]);
28 intc = points[i][1] + (-points[i][0]) * slope;
29 }
30 // cout << slope << ", " << intc << endl;
31
32 lineCounter[{slope, intc}].insert(i);
33 lineCounter[{slope, intc}].insert(j);
34 ans = max(ans, (int)lineCounter[{slope, intc}].size());
35 }
36 }
37
38 return ans;
39 }
40};
41
42//use coprime to represent slope!
43//Runtime: 32 ms, faster than 73.31% of C++ online submissions for Max Points on a Line.
44//Memory Usage: 9.9 MB, less than 55.26% of C++ online submissions for Max Points on a Line.
45class Solution {
46public:
47 int n;
48 map<pair<int, int>, int> lines;
49
50 pair<int, int> slope_coprime(int x1, int y1, int x2, int y2){
51 int dx = x1 - x2, dy = y1 - y2;
52 if(dx == 0){
53 //vertical line
54 return {0, 0};
55 }else if(dy == 0){
56 //horizontal line
57 return {INT_MAX, INT_MAX};
58 }
59
60 //the nominator must be >= 0
61 if(dx < 0){
62 dx *= -1;
63 dy *= -1;
64 }
65
66 int gcd = __gcd(dx, dy);
67
68 return {dx/gcd, dy/gcd};
69 };
70
71 void add_line(vector<vector<int>>& points, int i, int j, int &count, int& dup){
72 //update the map "lines", and update "count" and "dup"
73 int x1 = points[i][0];
74 int y1 = points[i][1];
75 int x2 = points[j][0];
76 int y2 = points[j][1];
77
78 if(x1 == x2 && y1 == y2){
79 //points[i] and points[j] are either same point
80 ++dup;
81 }else{
82 //or different point, then we need to calculate their line's slope
83 pair<int, int> slope = slope_coprime(x1, y1, x2, y2);
84 if(lines.find(slope) == lines.end()) lines[slope] = 2;
85 else ++lines[slope];
86 count = max(lines[slope], count);
87 }
88 };
89
90 int max_count_on_lines(vector<vector<int>>& points, int i){
91 //max count of points on a line passing through points[i]
92 lines.clear();
93 int count = 1;
94 int dup = 0; //count of points at same position as points[i]
95
96 //for a points[i], we only need to look after!!
97 for(int j = i+1; j < n; ++j){
98 add_line(points, i, j, count, dup);
99 }
100
101 return count + dup;
102 };
103
104 int maxPoints(vector<vector<int>>& points) {
105 n = points.size();
106 if(n <= 2) return n;
107
108 int max_count = 1;
109
110 for(int i = 0; i < n-1; ++i){
111 max_count = max(max_count, max_count_on_lines(points, i));
112 }
113
114 return max_count;
115 }
116};
Cost