-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path149.cpp
More file actions
55 lines (51 loc) · 1.47 KB
/
149.cpp
File metadata and controls
55 lines (51 loc) · 1.47 KB
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
//
// 149.cpp
// LeetCode
//
// Created by 张佐玮 on 15/9/1.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Max Points on a Line
//
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
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) {
unordered_map<double, int> slopes;
int result = 0;
for (int i = 0; i < points.size(); i++) {
slopes.clear();
int duplicates = 1, currentMax = 0;
for (int j = i + 1; j < points.size(); j++) {
int deltaX = points[j].x - points[i].x;
int deltaY = points[j].y - points[i].y;
if (!deltaY && !deltaX) {
duplicates++;
continue;
}
double currentSlope = numeric_limits<double>::infinity();
if (deltaX) {
currentSlope = (double) (deltaY) / (double) (deltaX);
}
if (slopes.find(currentSlope) == slopes.end()) {
slopes[currentSlope] = 1;
}
else {
slopes[currentSlope]++;
}
currentMax = max(currentMax, slopes[currentSlope]);
}
result = max(result, currentMax + duplicates);
}
return result;
}
};