LeetCode ---- Max Points On a Line

Source: Internet
Author: User

Max Points on a LineTotal Accepted: 4246Total Submissions: 42602

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


Analysis: Let's take a straight line composed of two points to see how many points are above it.

Note: consider three cases: Two-Point coincidence; the slope is infinite; normal

/** * Definition for a point. * struct Point { *     int x; *     int y; *     Point() : x(0), y(0) {} *     Point(int a, int b) : x(a), y(b) {} * }; */class Solution {public:    int maxPoints(vector
 
   &points) {        int max_points(0);        double a(0), b(0); // denote a straight line        LINE_TYPE line_type(LINE);                 if(points.size() == 0) return 0;        else if(points.size() == 1) return 1;                for(int i=0; i
  
   (points[j].x - points[i].x); //tangent                b = points[i].y - a * points[i].x;              }                            // compute how many points on the line                int point_count(2);              for(int k=0; k
   
    

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.