leetcode 149. 直线上最多的点数 解题报告
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。
示例 1:
输入: [[1,1],[2,2],[3,3]]
输出: 3
解释:
^
|
| o
| o
| o
+------------->
0 1 2 3 4
示例 2:
输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
输出: 4
解释:
^
|
| o
| o o
| o
| o o
+------------------->
0 1 2 3 4 5 6
#include<bits/stdc++.h>
using namespace std;
struct Point {
int x;
int y;
Point() : x(0), y(0) {}
Point(int a, int b) : x(a), y(b) {}
};
bool operator<(const Point &a, const Point &b) {
if(a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
bool operator==(const Point &a, const Point &b) {
return a.x == b.x and a.y == b.y;
}
int gcd(int a, int b) {
return b?gcd(b, a%b):a;
}
struct pair_hash {
template <class T1, class T2>
std::size_t operator () (const std::pair<T1,T2> &p) const {
auto h1 = std::hash<T1>{}(p.first);
auto h2 = std::hash<T2>{}(p.second);
return h1 ^ h2;
}
};
const pair<int, int> zero_pair = make_pair(0, 0);
pair<int,int> cmp(const Point &a, const Point &b) {
int c = b.y - a.y;
int d = b.x - a.x;
if(c == d && c == 0)
return make_pair(0,0);
if(c == 0)
return make_pair(0, 1);
if(d == 0)
return make_pair(1, 0);
int g = gcd(c, d);
return make_pair(c/g, d/g);
}
class Solution {
public:
int maxPoints(vector<Point>& points) {
if(points.size() < 2)
return points.size();
sort(points.begin(), points.end());
unordered_map<pair<int,int>,int, pair_hash>mp;
int ret = 2, sz = points.size();
for(int i = 0; i < sz; ++i) {
mp.clear();
for(int j = i + 1; j < sz; ++j) {
auto xy = cmp(points[i], points[j]);
if(mp.count(xy)) {
mp[xy] += 1;
} else
mp[xy] = 2;
if(mp.count(zero_pair) and xy != zero_pair) {
ret = max(ret, mp[xy] + mp[zero_pair] - 1);
} else
ret = max(ret, mp[xy]);
}
}
return ret;
}
};
int main() {
return 0;
}
leetcode 149. 直线上最多的点数 解题报告的更多相关文章
- Java实现 LeetCode 149 直线上最多的点数
149. 直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | ...
- Leetcode 149.直线上最多的点数
直线上最多的点数 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o ...
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
- [Swift]LeetCode149. 直线上最多的点数 | 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(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
随机推荐
- 2018.8.5 Bootstrap 使用
Bootstrap的环境搭建 <link rel="stylesheet" type="text/css" href="css/bootstra ...
- 关于VMware给系统分区扩容的一点经验
我的VMware版本是8.0.6 build-1035888,里面安装的是Windows XP SP3 首先,在VM关机状态下使用Hard disk设置中的Utilities下的Expand给整个磁盘 ...
- Scrivener 中文语言包
Scrivener 中文语言包 随着OS X EI Capitan的发布,Scrivener 也升级到了2.7,程序没有大的变化,主要是为了兼容10.11并更新了图标. 原来的2.6的中文语言包无法在 ...
- 基于指令的移植方式的几个重要概念的理解(OpenHMPP, OpenACC)-转载
引言: 什么是基于指令的移植方式呢?首先我这里说的移植可以理解为把原先在CPU上跑的程序放到像GPU一样的协处理器上跑的这个过程.在英文里可以叫Porting.移植有两种方式:一种是使用CUDA或者O ...
- centos 6.5 配置nginx环境
1.卸载系统中默认的php和httpd [root@x ~]# yum remove httpd* php* Loaded plugins: fastestmirror Setting up Remo ...
- BZOJ1061: [Noi2008]志愿者招募(线性规划)
Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 5725 Solved: 3437[Submit][Status][Discuss] Descript ...
- sqlite的sql常用语句(笔记)
1.复制一张表并重命名 比如已经创建好一个表 表名为"28165" 复制这个表. CREATE TABLE [33150] AS SELECT * FROM [28165] 2.根 ...
- kubernetes基础架构及原理
kubernetes简称“k8s” 其中“8”代表的是“k”和“s”中间的8个字母. k8s是Google公司开发的Borg项目中独立出来的容器编排工具,然后将其捐献给CNCF这个组织,然后发扬光大. ...
- Kernel list_head demo实现
内核中很多地方用到队列,如果每一个数据结构都实现一个双向队列,并针对这些数据结构实现对应的操作,那么代码将会非常冗余,于是内核抽象出了list_head数据结构,并文参考内核中的代码写成,实现了一个l ...
- 关于对GitHub的使用
什么是GitHub? GitHub是版本控制和协作的代码托管平台.它可以让你在其他人在任何地方一起工作. 本文主要向您介绍GitHub essentials,如存储库,分支,提交和合并请求.将您创建自 ...