forked from weiliu89/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxPointsOnALine.cpp
108 lines (95 loc) · 3 KB
/
maxPointsOnALine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Source : https://oj.leetcode.com/problems/max-points-on-a-line/
// Author : Hao Chen
// Date : 2014-10-12
/**********************************************************************************
*
* Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
*
**********************************************************************************/
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
struct Point {
int x;
int y;
Point() : x(0), y(0) {}
Point(int a, int b) : x(a), y(b) {}
};
// O(n^2) time complexity solution
int maxPoints(vector<Point> &points) {
#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)
if (points.size()<=0) return 0;
if (points.size()<=2) return points.size();
int maxnum = 0;
//using a map to find the same slope line
map<double, int> slopeMap;
//The algorithm here is quite straight forward.
// take each point in array to caculate with others
//
//Actually the algorithm here can be optimized.
// there are many duplicated calculation.
// considering two points A and B, (A,B) is same with (B,A), here re-calculated.
for(int i=0; i<points.size(); i++) {
//reset teh slope map.
slopeMap.clear();
slopeMap[INT_MIN] = 0;
int samePointCnt = 1;
for (int j=0; j<points.size(); j++) {
if (i==j) continue; //skip the same point
//Caculate the slope of two points
int delta_x = points[i].x - points[j].x;
int delta_y = points[i].y - points[j].y;
//Special case: two points are exactly at same place
if (delta_y == 0 && delta_x == 0){
samePointCnt++;
continue;
}
//Special case: delta_x == 0
double slope = INT_MAX;
if (delta_x!=0) {
slope = 1.0*delta_y / delta_x;
}
//Count the points is same line.
slopeMap[slope]++;
}
//find the max number of points located at same line with points[i]
map<double, int>::iterator it;
for (it = slopeMap.begin(); it != slopeMap.end(); it++) {
if (maxnum < it->second + samePointCnt) {
maxnum = it->second + samePointCnt;
}
}
}
return maxnum;
}
void generatePoints(vector<Point> &points, int n) {
srand(time(0));
Point p;
for(int i=0; i<n; i++) {
p.x = rand() % 1;
p.y = rand() % 1;
points.push_back(p);
}
}
void printPoints(vector<Point> &points) {
for(int i=0; i<points.size(); i++) {
cout << "(" << points[i].x << "," << points[i].y << ") ";
}
cout << endl;
}
int main(int argc, char** argv)
{
int n = 20;
if ( argc > 1) {
n = atoi(argv[1]);
}
vector<Point> points;
generatePoints(points, n);
printPoints(points);
cout << maxPoints(points) << endl;
return 0;
}