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 ...
随机推荐
- 【转】Android开发学习总结(一)——搭建最新版本的Android开发环境
最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是一直没有搭建开发环境去学习,Android的更新速度比较快了,Android1. ...
- CentOS系统中使用iptables设置端口转发
echo 1 > /proc/sys/net/ipv4/ip_forward 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0 ...
- 共变导数(Covariant Derivative)
原文链接 导数是指某一点的导数表示了某点上指定函数的变化率. 比如,要确定某物体的速度在某时刻的加速度,就取时间轴上下一时刻的一个微小增量,然后考察速度的增量和时间增量的比值.如果这个比值比较大,说明 ...
- 第一个AngularJS表达式实例
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- caffe的损失函数
损失函数,一般由两项组成,一项是loss term,另外一项是regularization term. J=L+R 先说损失项loss,再说regularization项. 1. 分对得分1,分错得分 ...
- Linux赋予普通用户root权限
本文以新建用户admin来举例,请自行替换自己需要的用户 方法一: vi编辑 /etc/sudoers 文件,找到 root ALL=(ALL) ALL,在下面添加一行,如下所示: ## ...
- 触发ionic弹窗区域外的方法
最近项目需要在页面弹窗的时候需要点击弹窗区域外的地方,其实也就是点击页面HTML就可以关闭弹窗, 首先在controller通过js获取到html的dom节点,然后绑定点击事件,话不多说上代码: ...
- 【赛时总结】◇赛时·VI◇ Atcoder ABC-104
◇赛时·VI◇ ABC-104 ◆??? 莫名爆炸……ABC都AK不了 QwQ C题竟然沦落到卡数据的地步:D题没有思路,直接放弃 ⋋( ◕ ∧ ◕ )⋌ ◆ 题目&解析 ◇A题◇ Rated ...
- yum 仓库配置
[base]name=aliyum basebaseurl=https://mirrors.aliyun.com/centos/6/os/x86_64/ ...
- Django 入门案例开发
Django是一个重量级的web开发框架,它提供了很多内部已开发好的插件供我们使用:这里不去描述 Django直接进入开发过程. Django入门案例分两部分:一.开发环境的配置:二.业务需求分析. ...