Algorithm {Max Points on a Line}

Today we will try to solve next problem from leetcode. The problem is Max Points on a Line.

Problem description:

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

So, that is short desciption…. Try Stop and Think

What do we have, we have input array/list of points and should return max number of the points on one line. We can try to remember school math course and one part is Collinear.

Three or more points P1,P2 ,P3 , …, are said to be collinear if they lie on a single straight line .

Hm, this is our case 🙂, we should find max points on the one line. Ok, Lets go to implement it.

So, that is easy, with one trick , is checking if all input array contains the same points.

Other cases are easy to understand, run 3 loops (to get 3 Points) check if the points different and if collinear (lay on the one line) increase count of the points, after that check with max value, if count > max assign max to current count and try other points.

The complexity of current implementation is O (N^3), memory O (1) 🙁,  but leetcode accepted that solution, so this is good, but…

Can we improve current solution?

Generally yes, we can use some container like Map, for storing lines and count of the Points on that line and after that just choose what line contains max count of Points. My friend solved the problem using that approach, Complexity will be O (N^2) and memory usage O (N).


READ  Compare Best HTML Parsers in Java

Leave a Reply

Your email address will not be published. Required fields are marked *