# LeetCode Max Points on a Line: Hashing

## Overview

LeetCode Max Points on a Line could be solved in O(N^2) time by find the maximum point set containing i in O(N) time (Hash) and get the maximum among all i’s.

## LeetCode Max Points on a Line

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

## Analysis: Hashing

1) Naive solution: for each pair of nodes, we determine the line cross them, and test this line against all the points to check whether the point is on this line or not. Obviously, this is O(N^3) solution. Although not efficient, it could pass the LeetCode OJ anyways to solve Max Points on a Line problem:

Special care should be taken of to handle the duplicated points in the input which is the very tricky part and such repeated points need to be counted into the results!

2) O(N^2) solution using Hashing: So can we come up with more efficient solution? Of course! The key is that we can actually find the maximum number of point set that contains point i in O(N) time. That is, for each point in the input, we can calculate the maximum number of points on the same line across point i, and this could be done in O(N) time by using Hashing table. And among all these “local” maximum points, we return the “global” maximum among them (really a bit of DP flavor), then we are good! So let’s see how can we determine the the maximum point set for point i in O(N) time.

We need some math concepts of lines. We know that a line could be determined by the slop and a point on that line. So for point i, we calculate the slope of the lines determined by point and any other point j != i and each time we store this slope into a hash table: if the slope determined by point and j is already in hash table, this means, the current point j is on the same line which is determined by point i and some previous point, so we update the value of the hash able entry (slope, count) where count just keeps the number of such points on the line with slope and point i. All of these could be done in constant time using the hash table, and we get the local maximum for point i.

The following is the source code accepted by LeetCode OJ to pass this Max Points on a Line problem:

Several noteworthy points are:

1. I degined a customized class called Slope because using double as a map or hash table key is very dangrous for the double arithmatic is inaccurate and the results could actually be unpredictable.
2. The above code using map is implemented by red black tree, so the real time complexity is O(N^2logN), the thing I did not use unordered_map is because it seems that LeetCode OJ does not support g++ –std=c++0x option.
3. Again, the duplicated points in the input need to be handled carefully as special cases
4. Do not be concerned with those vertical lines, think about why.
5. There is even more efficient implementation by only examining the current testing point against all the other points on its right side in the input points array, I actually do not fully get that idea? You are welcome to leave your comment about this!

## Summary

LeetCode Max Points on a Line could be solved in O(N^2) time by find the maximum point set containing i in O(N) time (Hash) and get the maximum among all i’s.

Written on December 20, 2014