Skip to content

Commit 829b2d0

Browse files
author
dtsipc
committed
update
1 parent 8d8da34 commit 829b2d0

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
File renamed without changes.

3-max_points_on_a_line.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
class Solution {
11+
public:
12+
int maxPoints(vector<Point> &points) {
13+
if(points.size() < 3) {
14+
return points.size();
15+
}
16+
int maxPoints = 0; //the max point in line
17+
int size = points.size();
18+
map<double, int> count;
19+
map<double, int>::iterator iter;
20+
for(int i = 0; i < size; i++ ) {
21+
int x1 = points[i].x;
22+
int y1 = points[i].y;
23+
int coincideCount = 0; //number of duplicate points
24+
count.clear();
25+
count[(double)INT_MIN] = 0;
26+
for(int j = i + 1; j < size; j++) {
27+
int x2 = points[j].x;
28+
int y2 = points[j].y;
29+
if(x1 == x2 && y1 == y2) {
30+
coincideCount++;
31+
} else if(x1 == x2){
32+
count[(double)INT_MIN]++;
33+
} else {
34+
double slope = 1.0*(y1-y2)/(x1-x2);
35+
count[slope]++;
36+
}
37+
}
38+
for(iter = count.begin(); iter != count.end(); iter++) {
39+
if(iter->second + coincideCount > maxPoints) {
40+
maxPoints = iter->second + coincideCount;
41+
}
42+
}
43+
}
44+
maxPoints = maxPoints + 1;
45+
return maxPoints;
46+
}
47+
};
48+
49+
50+
51+
/*
52+
#include <vector>
53+
#include <iostream>
54+
#include <stdlib.h>
55+
#include <sstream>
56+
#include <limits.h>
57+
#include <map>
58+
59+
using namespace std;
60+
61+
struct Point {
62+
int x;
63+
int y;
64+
Point() : x(0), y(0) {}
65+
Point(int a, int b) : x(a), y(b) {}
66+
};
67+
68+
int maxPoints(vector<Point> &points) {
69+
if(points.size() < 3) {
70+
return points.size();
71+
}
72+
int maxPoints = 0; //the max point in line
73+
int size = points.size();
74+
map<double, int> count;
75+
map<double, int>::iterator iter;
76+
for(int i = 0; i < size; i++ ) {
77+
int x1 = points[i].x;
78+
int y1 = points[i].y;
79+
int coincideCount = 0; //number of duplicate points
80+
count.clear();
81+
count[(double)INT_MIN] = 0;
82+
for(int j = i + 1; j < size; j++) {
83+
int x2 = points[j].x;
84+
int y2 = points[j].y;
85+
if(x1 == x2 && y1 == y2) {
86+
coincideCount++;
87+
} else if(x1 == x2){
88+
count[(double)INT_MIN]++;
89+
} else {
90+
double slope = 1.0*(y1-y2)/(x1-x2);
91+
count[slope]++;
92+
}
93+
}
94+
for(iter = count.begin(); iter != count.end(); iter++) {
95+
if(iter->second + coincideCount > maxPoints) {
96+
maxPoints = iter->second + coincideCount;
97+
}
98+
}
99+
}
100+
maxPoints = maxPoints + 1;
101+
return maxPoints;
102+
}
103+
int main() {
104+
Point p1 = Point(1,1);
105+
Point p2 = Point(2,2);
106+
Point p3 = Point(4,3);
107+
Point p4 = Point(4,4);
108+
vector<Point> points;
109+
points.push_back(p1);
110+
points.push_back(p2);
111+
points.push_back(p3);
112+
points.push_back(p4);
113+
114+
int maxNumber = maxPoints(points);
115+
cout<<maxNumber<<endl;
116+
117+
return 0;
118+
}
119+
120+
*/

max_points_on_a_line.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)