File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for a point.
3
+ * struct Point {
4
+ * int x;
5
+ * int y;
6
+ * Point() : x(0), y(0) {}
7
+ * Point(int a, int b) : x(a), y(b) {}
8
+ * };
9
+ */
10
+
11
+ bool operator <(const Point& x, const Point& y) {
12
+ if (x.x == y.x ) {
13
+ return x.y < y.y ;
14
+ }
15
+ return x.x < y.x ;
16
+ }
17
+
18
+ bool operator ==(const Point& x, const Point& y) {
19
+ if (x.x == y.x && x.y == y.y ) {
20
+ return true ;
21
+ }
22
+ return false ;
23
+ }
24
+
25
+ class Solution {
26
+ public:
27
+ int maxPoints (vector<Point> &points) {
28
+ int n = points.size ();
29
+ if (n <= 2 ) {
30
+ return n;
31
+ }
32
+ int result = 2 ;
33
+ sort (points.begin (), points.end ());
34
+ for (int i = 0 ; i < n; i++) {
35
+ int dup = 1 ;
36
+ while (i + 1 < n && points[i+1 ] == points[i]) {
37
+ i++;
38
+ dup++;
39
+ }
40
+ result = max (result, dup);
41
+ if (i + 1 == n) {
42
+ return result;
43
+ }
44
+ vector<double > slopes;
45
+ for (int j = i + 1 ; j < n; j++) {
46
+ if (points[j].x == points[i].x ) {
47
+ slopes.push_back (numeric_limits<double >::max ());
48
+ } else {
49
+ slopes.push_back ((double )(points[j].y - points[i].y ) / (points[j].x - points[i].x ));
50
+ }
51
+ }
52
+ sort (slopes.begin (), slopes.end ());
53
+ int count = 1 ;
54
+ result = max (result, dup + 1 );
55
+ for (int k = 1 ; k < slopes.size (); k++) {
56
+ if (slopes[k] == slopes[k-1 ]) {
57
+ count++;
58
+ } else {
59
+ count = 1 ;
60
+ }
61
+ result = max (result, count + dup);
62
+ }
63
+ }
64
+ return result;
65
+ }
66
+ };
You can’t perform that action at this time.
0 commit comments