Reputation: 10661
Given: a lot of points each with a unique coordinate (xi,yi)
Output: Max number of points on the same line
This is my method:
for i=1..n
for j=i..n
get the line determined by point[i] and point[j]
for k=1..n
check if point[k] is on this line
But it seems this method takes too much time and always exceeds the time limit on the OJ system.
Upvotes: 1
Views: 283
Reputation: 2434
I have not implemented this, but you might be able to do it in O(n).
Use a hashmap to store the points by their polar angles: Map<Double,List<Point>>
, where Double
is the angle
Then iterate the map, tracking the List<Point>
with the max length. That list would contain the result.
Play with that. It looks like it should work.
Upvotes: 0
Reputation: 927
iterate each point, calculate the polar angle for each other point, sort the polar angle
this cost O(n^2*lgn)
Upvotes: 3