- 15.5%

https://leetcode.com/problems/max-points-on-a-line/#/description

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

https://discuss.leetcode.com/topic/6028/sharing-my-simple-solution-with-explanation

Sharing my simple solution with explanation

1 | int maxPoints(vector<Point> &points) { |

First, let’s talk about mathematics.

How to determine if three points are on the same line?

The answer is to see if slopes of arbitrary two pairs are the same.

Second, let’s see what the minimum time complexity can be.

Definitely, O(n^2). It’s because you have to calculate all slopes between any two points.

Then let’s go back to the solution of this problem.

In order to make this discussion simpler, let’s pick a random point A as an example.

Given point A, we need to calculate all slopes between A and other points. There will be three cases:

Some other point is the same as point A.

Some other point has the same x coordinate as point A, which will result to a positive infinite slope.

General case. We can calculate slope.

We can store all slopes in a hash table. And we find which slope shows up mostly. Then add the number of same points to it. Then we know the maximum number of points on the same line for point A.

We can do the same thing to point B, point C…

Finally, just return the maximum result among point A, point B, point C…

https://discuss.leetcode.com/topic/2709/c-o-n-2-solution-for-your-reference

Hint by @stellari

“For each point pi, calculate the slope of each line it forms with all other points with greater indices, i.e. pi+1, pi+2, …, and use a map to record how many lines have the same slope (If two lines have the same slope and share a common point, then the two lines must be the same one). By doing so, you can easily find how many points are on the same line that ends at pi in O(n). Thus the amortized running time of the whole algorithm is O(n^2).”

In order to avoid using double type(the slope k) as map key, I used pair (int a, int b) as the key where a=pj.x-pi.x, b=pj.y-pi.y, and k=b/a. Using greatest common divider of a and b to divide both a, b ensures that lines with same slope have the same key.

I also handled two special cases: (1) when two points are on a vertical line (2) when two points are the same.

1 | class Solution { |

https://discuss.leetcode.com/topic/12877/20-line-c-o-n-2-hashing-solution

The idea is straight forward. Calculate each slope between two points and handle two special cases: 1. vertical, 2. duplicate.

1 | class Solution { |

https://discuss.leetcode.com/topic/18447/16ms-28ms-c-solutions-with-explanations

16ms/28ms C++ Solutions with Explanations

This problem has a naive idea, which is to traverse all possible pairs of two points and see how many other points fall in the line determined by them. This idea is of O(n^3) time complexity and will meet TLE.

Well, let’s focus on lines instead of pairs of points. Could we just find out how many points fall in all possible lines? The answer is yes. Remember that a line is determined by its slope and intercept. In fact, if two lines with the same slope share a common point, then they are just the same line. So to determine a line, we need its slope and a point.

Now comes the idea to solve this problem. We start from a specific point p, and compute all the slopes of the lines between p and the remaining points. Then those with the same slopes will be the same line. We can find out the maximum number of points fall on a line containing p. We exhaust all possible p’s and record the largest number we have seen. This number is just answer.

Well, there are still two special cases to handle:

- Duplicate points: a pair of duplicate points give no determined line, so we just count the number of duplicates and add them to the result.
- Vertical lines: the slope of these lines is infinity mathematically. We simply set it to be INT_MAX in the following code.

Now we have the following code, using an unordered_map<float, int> slopes to record how many points fall in the line of a specific slope and containing points[i]. Since all the operations of unordered_map is O(1), this code is of O(n^2) complexity.

1 | class Solution { |

Well, since the representation of floating point numbers is sometimes inaccurate, we may use a more safer way to represent the slope (dy / dx), which is to record dx and dy in a pair<int, int>. However, once we use pair<int, int> for the key of the map, we cannot use an unordered_map since pair<int, int> is unhashable. We now change to map and the time complexity becomes O(n^2logn). Also, since dy = 4, dx = 2 and dy = 8, dx = 4 represents the same slope, we need to divide both of them by their gcd first.

The code is as follows. The logic is the same of the one above, just introducing pair and gcd.

1 | class Solution { |

https://discuss.leetcode.com/topic/21896/python-68-ms-code

Python 68 ms code

1 | def maxPoints(self, points): |

https://discuss.leetcode.com/topic/2979/a-java-solution-with-notes

A line is determined by two factors,say y=ax+b

If two points(x1,y1) (x2,y2) are on the same line(Of course).

Consider the gap between two points.

We have (y2-y1)=a(x2-x1),a=(y2-y1)/(x2-x1) a is a rational, b is canceled since b is a constant

If a third point (x3,y3) are on the same line. So we must have y3=ax3+b

Thus,(y3-y1)/(x3-x1)=(y2-y1)/(x2-x1)=a

Since a is a rational, there exists y0 and x0, y0/x0=(y3-y1)/(x3-x1)=(y2-y1)/(x2-x1)=a

So we can use y0&x0 to track a line;

1 | /** |

https://discuss.leetcode.com/topic/24011/accepted-java-solution-easy-to-understand

1 | /** |