[Leetcode] Max Points on a Line, leetcodepoints

Source: Internet
Author: User

[Leetcode] Max Points on a Line, leetcodepoints
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.

Solution:

1. First, an O (n ^ 3) method is used to calculate the equation (n ^ 2) of each line, and then determine the number of points on each line (N ). This method must be feasible, but the complexity is too high.
2. then we think of an O (N), and calculate the slope of this vertex and all other vertices for each vertex, which is a straight line composed of the points with the same slope, is a straight line with the most vertices.

Note:

1.Point of coincidence
2.Point where the slope does not exist

 1 # Definition for a point 2 class Point: 3     def __init__(self, a=0, b=0): 4         self.x = a 5         self.y = b 6  7 class Solution: 8     # @param points, a list of Points 9     # @return an integer10     def calcK(self,pa, pb):11         t = ((pb.y - pa.y) * 1.0) / (pb.x - pa.x)12         return t13         14     def maxPoints(self, points):15         l = len(points)16         res = 017         if l <= 2:18             return l 19         for i in xrange(l):20             same = 021             k = {}22             k['inf'] = 023             for j in xrange(l):24                 if points[j].x == points[i].x and points[j].y != points[i].y:25                     k['inf'] += 126                 elif points[j].x == points[i].x and points[j].y == points[i].y:27                     same +=128                 else:29                     t = self.calcK(points[j],points[i])30                     if t not in k.keys():31                         k[t] = 132                     else:33                         k[t] += 134             res = max(res, max(k.values())+same)35         return res36         37 def main():38     points = []39     points.append(Point(0,0))40     points.append(Point(1,1))41     points.append(Point(1,-1))42     #points.append(Point(0,0))43     #points.append(Point(1,1))44     #points.append(Point(0,0))45     s = Solution()46     print s.maxPoints(points)47     48     49 if __name__ == '__main__':50     main()51         

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.