GivenNPoints on a 2D plane, find the maximum number of points that lie on the same straight line.
1st (7 tries)
Class solution {public: int maxpoints (vector <point> & points) {unordered_map <float, int> kmap; int maxres = 0; For (INT I = 0; I <points. size (); I ++) {int same_num = 1; kmap. clear (); kmap [int_min] = 0; For (Int J = 0; j <points. size (); j ++) {If (j = I) continue; If (points [I]. X = points [J]. X & points [I]. y = points [J]. y) {same_num ++; continue;} float K = points [I]. X = points [J]. x? Int_max :( float) (points [J]. y-points [I]. y)/(points [J]. x-points [I]. x); kmap [k] ++;} unordered_map <float, int >:: iterator ITER; For (iter = kmap. begin (); iter! = Kmap. end (); ++ ITER) {If (ITER-> second + same_num> maxres) {maxres = ITER-> second + same_num ;}} return maxres ;}};
2nd (4 tires)
/*** 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 <point> & points) {int max_p = 0; for (unsigned I = 0; I <points. size (); I ++) {Map <double, int> slopes; int min_p = 0; For (unsigned J = 0; j <points. size (); j ++) {If (points [I]. X = points [J]. X & points [I]. y = points [J]. y) {Min_p ++; continue;} Double K = (points [I]. X-points [J]. X )? (Double) (points [I]. y-points [J]. y)/(points [I]. x-points [J]. x): int_max; If (slopes. find (K) = slopes. end () Slopes [k] = 1; else slopes [k] ++;} If (slopes. size () = 0) {If (min_p> max_p) max_p = min_p;} else {for (Map <double, int >:: iterator iter = slopes. begin (); iter! = Slopes. end (); ++ ITER) {If (ITER-> second + min_p> max_p) max_p = ITER-> second + min_p ;}} return max_p ;}};