Max Points on a Line (6)

Source: Internet
Author: User

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

How can we determine a point on the same line? You can do this. Set an initial start point to calculate the slope of the straight line from the remaining point to the starting point. If the slope is the same, the points are on the same line. However, we also need to consider the absence of slope and repetition points. Therefore, the number and slope of repeat points are recorded separately as positive infinity. How can we obtain the final maximum value? It should be the number of points with the same slope, the number of repeated points and the number of lines in the vertical line, and the maximum number of repeated points, which should be clear for a while. To quickly obtain the number of straight lines with the same slope value, map is used in this example. Given the slope value, the corresponding correlation value can be quickly found, which is convenient and fast, you don't have to write a loop.

It was a bit embarrassing. After a long time, I finally finished it. Afterwards, I found that it was because I mistakenly wrote = in the if judgment condition. It was a bit sad! The ac will soon be available!

Paste the code now!

1 class Solution {2 public: 3 int maxPoints (vector <Point> & points) {4 if (points. size () <= 2) 5 return points. size (); 6 int I, j; 7 int coin; 8 int h; 9 int maxsize =-1; 10 double k; 11 for (I = 0; I <points. size ()-1; I ++) {12 map <double, int> point; 13 map <double, int>: iterator it; 14 h = 0; 15 coin = 1; // you have already done so. Because every line is formed, points start from 2! 16 for (j = I + 1; j <points. size (); j ++) {17 if (points [j]. x = points [I]. x & points [j]. y = points [I]. y) {18 coin ++; 19 continue; 20} 21 else if (points [j]. x = points [I]. x) {// This is where the = is converted to =! 22 h ++; 23 continue; 24} 25 else if (points [j]. y = points [I]. y) {26 k = 0.0; 27} 28 else {29 k = (double) points [j]. y-points [I]. y; 30 k/= (double) (points [j]. x-points [I]. x); 31} 32 it = point. find (k); 33 if (it = point. end () {34 point. insert (map <double, int >:: value_type (k, 1); 35} 36 else {37 point [k] ++; 38} 39 40} 41 h + = coin; // h must be greater than coin42 for (it = point. begin (); it! = Point. end (); it ++) {43 int temp = it-> second + coin; 44 if (temp> maxsize) 45 maxsize = temp; 46 47} 48 if (h> maxsize) 49 maxsize = h; 50 51} 52 53 return maxsize; 54 55 56} 57 };

 

Max Points on a Line (6)

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.