[LintCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Given 4 points: (1,2)
, (3,6)
, (0,0)
, (1,3)
.
The maximum number is 3
.
LeetCode上的原题,请参见我之前的博客Max Points on a Line。
解法一:
class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = , n = points.size();
for (int i = ; i < n; ++i) {
int duplicate = ;
unordered_map<double, int> m;
for (int j = i + ; j < n; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
++duplicate;
} else if (points[i].x == points[j].x) {
++m[INT_MAX];
} else {
double slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x);
++m[slope];
}
}
res = max(res, duplicate);
for (auto it : m) {
res = max(res, it.second + duplicate);
}
}
return res;
}
};
解法二:
class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = , n = points.size();
for (int i = ; i < n; ++i) {
int duplicate = ;
map<pair<int, int>, int> m;
for (int j = i + ; j < n; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
++duplicate; continue;
}
int dx = points[j].x - points[i].x;
int dy = points[j].y - points[i].y;
int d = gcd(dx, dy);
++m[{dx / d, dy / d}];
}
res = max(res, duplicate);
for (auto it : m) {
res = max(res, it.second + duplicate);
}
}
return res;
}
int gcd(int a, int b) {
return (b == ) ? a : gcd(b, a % b);
}
};
解法三:
class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = ;
for (int i = ; i < points.size(); ++i) {
int repeat = ;
for (int j = i + ; j < points.size(); ++j) {
int cnt = ;
int x1 = points[i].x, y1 = points[i].y;
int x2 = points[j].x, y2 = points[j].y;
if (x1 == x2 && y1 == y2) {++repeat; continue;}
for (int k = ; k < points.size(); ++k) {
int x3 = points[k].x, y3 = points[k].y;
if (x1 * y2 + x2 * y3 + x3 * y1 - x3 * y2 - x2 * y1 - x1 * y3 == ) {
++cnt;
}
}
res = max(res, cnt);
}
res = max(res, repeat);
}
return points.empty() ? : res;
}
};
[LintCode] Max Points on a Line 共线点个数的更多相关文章
- [LeetCode] Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- [LeetCode] 149. Max Points on a Line 共线点个数
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 【leetcode】Max Points on a Line
Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- 【LeetCode】149. Max Points on a Line
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- LeetCode: Max Points on a Line 解题报告
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- Max Points on a Line leetcode java
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
- [leetcode]149. Max Points on a Line多点共线
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 【Max Points on a Line 】cpp
题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...
随机推荐
- WPF应用程序exe接收参数
using System;using System.ServiceProcess; namespace GoShopService{ public partial class Service1 ...
- ORA-19602: cannot backup or copy active file in NOARCHIVELOG mode
备份数据库,报错如下 RMAN> backup database; Starting backup at -JAN- allocated channel: ORA_DISK_1 channel ...
- 1分钟试用PowerShell 5.0新功能PowerShellGet安装Script Browser和Script Analyzer
微软PowerShell 产品组上周发布了PowerShell 5.0 PowerShellGet功能.有了它,IT 人员可以方便地搜索,安装,更新PowerShell Module.在这篇博客中,我 ...
- Authentication 方案优化探索(JWT, Session, Refresh Token, etc.)
转载自:http://www.jianshu.com/p/5ac8a0e1e5a8
- 基于CentOS体验万象优图鉴黄服务
系统要求:CentOS 7.2 64 位操作系统 初始化配置 使用万象优图图片鉴黄 API 接口,我们需要先完成以下步骤: 获取腾讯云账号 APP ID 配置云 API 公钥/密钥 配置优图 buck ...
- texlive测试是否安装成功
在完成TEX Live安装之后,自然你会希望试试看它是否正常工作,好让你在以后能够创建优美的文档和字体. 1.首先确认你可以执行tex程序: >tex --version TeX 3.14159 ...
- python文件的基础操作
import os print(,'-')) print(os.getcwd()) print(,'-')) print(os.listdir()) print(,'-')) print(os.lis ...
- pandas删除缺失数据(pd.dropna()方法)
1.创建带有缺失值的数据库: import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(5, 3), ind ...
- Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand
1 ItemDataBound:数据绑定的时候(正在进行时)发生. 2 ItemCommand :用来响应Item模板中的控件的事件. 如下代码 aspx代码: [html] view plain c ...
- 模板, 保存&发布
单击“视图”菜单中的“幻灯片模板”按钮,会弹出“幻灯片母版”选项卡,在此选项卡中,可以修改当前PPT的模板, 例如网上下载的幻灯片上的LOGO都可在这里去除. 模板设计总结 1. 背景可以选择纯色,也 ...