Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
max-points-on-a-line.cpp 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-06-09 18:53 +08:00 . Create max-points-on-a-line.cpp
// Time: O(n^2)
// Space: O(n)
/**
* Definition for a point.
* 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) {
int max_points = 0;
for (int i = 0; i < points.size(); ++i) {
unordered_map<double, int> slope_count;
const auto& start = points[i];
int same = 1;
for (int j = i + 1; j < points.size(); ++j) {
const auto& end = points[j];
if (start.x == end.x && start.y == end.y) {
++same;
} else {
auto slope = numeric_limits<double>::max();
if (start.x - end.x != 0) {
slope = (start.y - end.y) * 1.0 / (start.x - end.x);
}
++slope_count[slope];
}
}
int current_max = same;
for (const auto& kvp : slope_count) {
current_max = max(current_max, kvp.second + same);
}
max_points = max(max_points, current_max);
}
return max_points;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助