1
- #include < iostream>
2
- #include < vector>
3
- #include < unordered_map>
4
- #include < climits>
5
- #include < map>
6
- using namespace std ;
1
+ class Solution {
2
+ public:
3
+ int maxPoints (vector<vector<int >>& points) {
4
+ int res = 1 , n = points.size ();
5
+ unordered_map<int , int > lines;
6
+ for (int i = 0 ; i < n; i++) {
7
+ lines.clear ();
8
+ for (int j = i + 1 ; j < n; j++) {
9
+ auto & p1 = points[i];
10
+ auto & p2 = points[j];
7
11
8
-
9
- // Definition for a point.
10
- struct Point {
11
- int x;
12
- int y;
13
- Point () : x(0 ), y(0 ) {}
14
- Point (int a, int b) : x(a), y(b) {}
15
- };
16
-
17
- static int x = []() {std::ios::sync_with_stdio (false ); cin.tie (0 ); return 0 ; }();
18
- class Solution
19
- {
20
- public:
21
- int maxPoints (vector<Point > &points)
22
- {
23
- if (points.size () < 2 ) return points.size ();
24
- int result = 0 ;
25
- map<pair<int , int >, int > lines;
26
- for (unsigned int i = 0 ; i < points.size (); i++) {
27
- lines.clear ();
28
- int duplicate = 1 , vertical = 1 ;
29
- for (unsigned int j = i + 1 ; j < points.size (); j++)
30
- {
31
- if (points[j].x == points[i].x && points[j].y == points[i].y )
32
- {
33
- duplicate++;
34
- continue ;
35
- }
36
- else if (points[j].x == points[i].x ) vertical++;
37
- else
38
- {
39
- int a = points[j].x - points[i].x ;
40
- int b = points[j].y - points[i].y ;
41
- int gcd = GCD (a, b);
42
- a /= gcd; b /= gcd;
43
- lines[make_pair (a, b)]++;
44
- }
45
- }
46
- if (result < duplicate) result = duplicate;
47
- if (result < vertical) result = vertical;
48
- for (auto & line : lines)
49
- {
50
- int tmp = duplicate + line.second ;
51
- if (result < tmp) result = tmp;
52
- }
53
- }
54
- return result;
55
- }
56
- private:
57
- int GCD (int a, int b)
58
- {
59
- if (b == 0 ) return a;
60
- else return GCD (b, a % b);
12
+ auto a = p1[0 ] - p2[0 ];
13
+ auto b = p1[1 ] - p2[1 ];
14
+ auto c = GCD (a, b);
15
+ a /= c, b /= c;
16
+ lines[a * 20000 + b]++;
17
+ }
18
+ for (auto & [x, y] : lines) {
19
+ res = max (res, y + 1 );
20
+ }
61
21
}
62
- };
63
- int main ()
64
- {
65
- return 0 ;
66
- }
22
+ return res;
23
+ }
24
+ int GCD (int a, int b) { return b == 0 ? a : GCD (b, a % b); }
25
+ };
0 commit comments