LeetCode: Max Points on a Line

求二维平面上共线最多的点数 思想为:统计每个点的各个斜率的直线上有多少个点 C++代码: #include #include #include #include #include // 随机数通用公式:a + rand() % n;其中的a是起始值,n是整数的范围 /* 要取得[a,b)的随机整数,使用(rand() % (b-a))+ a; 要取得[a,b]的随机整数,使用(rand() % (b 阅读详情

这个设计test case是个好题目。本来的想法是以斜率为key,做hashtable计数,试了几次行不通。似乎改造下这个方案也是可行的。


后来干脆按照line来计数。最主要的是点重复这个事情不好弄,干脆吧点集预处理下。发现leetcode论坛里有个类似的思路,不过那个解法错了。

代码有点丑,如下:

struct Point {
	int x;
	int y;
	Point() : x(0), y(0) {}
	Point(int a, int b) : x(a), y(b) {}
};

struct Line {
	double s;
	double t;
	Line(Point a, Point b){
		if (a.x==b.x){
			s = numeric_limits<double>::max();
			t = a.x;
		}
		else{
			s  = (b.y*1.0 - a.y) / (b.x-a.x);
			t = a.y - a.x*s;
		}
	}
};

bool operator<(const Line& a, const Line& b){
	return a.s == b.s ? a.t < b.t : a.s < b.s;
}

bool operator<(const Point&a, const Point& b){
	return a.x == b.x ? a.y < b.y : a.x < b.x;
}

map<Point, int>::iterator insertOrUpdate(map<Point, int>& m, Point p){
	auto it = m.find(p);
	if (it==m.end()){
		auto res = m.insert(make_pair(p, 1));
		it = res.first;
	}
	else{
		it->second++;
	}
	return it;
}

map<Line, int>::iterator insertOrUpdate(map<Line, int>& m, Line l, int count){
	auto it = m.find(l);
	if (it == m.end()){
		auto res = m.insert(make_pair(l, count));
		it = res.first;
	}
	else{
		it->second+=count;
	}
	cout << it->second << endl;
	return it;
}

int maxPoints(vector<Point> &points) {
	if (points.size()==1){
		return 1;
	}
	map<Point, int> pCount;
	set<Point> pSet;
	int result = 0;
	for (auto& i:points){
		auto it = insertOrUpdate(pCount, i);
		pSet.insert(i);
		result = max(result, it->second);
	}
	map<Line, set<const Point*>> lineMap;
	for (auto i = pSet.begin(); i != pSet.end(); ++i){
		auto j = i;
		j++;
		for (; j != pSet.end(); ++j){
			Line line(*i, *j);
			lineMap[line].insert(&(*i));
			lineMap[line].insert(&(*j));
		}
	}
	for (auto& i:lineMap){
		int tmp = 0;
		for (auto& j:i.second){
			tmp += pCount[*j];
		}
		result = max(result, tmp);
	}
	return result;
}


AI数字人短视频数字人口播数字人分身建模流程全解析 在人工智能与计算机图形学蓬勃发展的当下,数字人建模作为打造虚拟数字人的核心环节,其流程复杂且精妙,融合了多种前沿技术与创意构思,为栩栩如生的数字人形象奠定基石。​。 阅读详情

相关推荐

手把手教你用Arduino IDE玩转ESP32-S3-wroom(附串口调试技巧)

本文详细介绍了如何在Arduino IDE环境下配置和使用ESP32-S3-WROOM模块,包括开发环境搭建、硬件连接、程序烧录及高级调试技巧。通过实战步骤和常见问题解决方案,帮助开发者快速上手这一高性能Wi-Fi/蓝牙双模模组,提升物联网开发效率。

weixin_30487201的博客 296

LightOJ1292->求最多共线点数

LightOJ1292->计算几何题意: 给出平面上n个点,要求最多有多少个点能共线。 题解: 使用map存储每个点相对于原点的不同斜率上对应的点的个数,最后输出最大值即可。 代码:#include <stdio.h> #include <iostream> #include <map> using namespace std ; int GCD(int a , int b) {return

忘川蒿里 1000

航天仿真必看:如何用SOFA库实现高精度GCRS-ITRS坐标转换?避坑指南

本文深入解析了航天仿真中高精度GCRS-ITRS坐标转换的关键技术,重点介绍了SOFA库在天球系与轨道系转换中的权威应用。通过对比传统方法的误差来源,详细阐述了SOFA库的转换算法优势、工程实现步骤及误差控制策略,为航天器轨道仿真提供毫米级精度的解决方案。

7h6j5k4l3的博客 852

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. 任意一条直线都可以表述为 y = ax + b 假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有 y1 = kx1 +b y2 = kx2 +b 由此

ZkvIA的博客 2万+

fMRI原始数据分割的笔记

好记性不如烂笔头_fMRI小白入门笔记

ZY_brain的博客 2905

无法获得下列放可solidworks standard 无法连接到服务器-15,10,10061如何解决?

无法获得下列放可solidworks standard 无法连接到服务器-15,10,10061如何解决?

qq_63539957的博客 2万+

LeetCode: Max Points on a Line [149]

【题目】 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 【题意】 给定一堆点,要求找出一条之前上的最大点数 【思路】 没什么好的方法,从每个点P出发,遍历所有的情况 从每个点P出发,斜率相同的点即为统一之前上的点 注意两种特殊情况: 1. 两个点重合(即为同一个点)

Harry Huang 1230

一根同轴线,真的扛得住 4K 吗? ——从摄像头带宽算起,聊透车载 SerDes 接口选型

如果你记住这三句话,这篇文章就值了:🎯摄像头带宽 = 分辨率 × 位深 × fps × 开销4K RAW 摄像头 ≈ 3.7Gbps,是现实不是理论SerDes 选型,宁可多留 20%,不要刚刚好。

weixin_43199439的博客 196

LeetCode:Max Points on a Line

Q: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 思路:n个点,最多形成 (n-1)! 条线段,利用2个点p[i],p[j](j=i+1)可以得到它们所有的所属直线的斜率.以该斜率为key, Set为value,set中存放的是构成

Leo's Blog 1127

七天征服AutoSAR/SomeIP_04理解SoAD_LdCom_SD模块

本文深入解析了AUTOSAR平台下SOME/IP协议栈的架构设计与模块交互逻辑。重点剖析了SD模块(服务发现)和LdCom模块(通信适配层)与SoAD(Socket适配层)之间的复杂关联关系。文章通过详细配置表格和代码示例,阐述了服务注册发现机制、PDU路由控制策略、数据收发流程等核心功能,并形象地将三模块关系类比为"三极管结构"。特别强调了SD模块通过RoutingGroup实现通信路径的动态开关控制,以及LdCom模块在协议栈中的透传转发作用,帮助开发者理解配置规则背后的底层原理,提

大叔带刺的博客 215

C#的GDI+绘图详解

/消除锯齿 或 AntiAlias。// 1.在窗体或控件的Paint事件中,用PaintEventArgs e 创建绘图对象 ---- 控件或窗体重绘时。//为控件创建绘图对象。//--------------------以上方法三选一。//2.创建绘图对象 panel1中绘图。

weixin_50920631的博客 3464

LeetCode:149 Max Points on a Line

描述: Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line. Example 1: Input: [[1,1],[2,2],[3,3]] Output: 3 Explanation: ^ | | o | o | o +...

Sword1996的博客 355

MNI152 坐标空间和 fsaverage 坐标空间相互转换, 如已知fsaverage空间的坐标如何求MNI152 坐标?aparc.a2009s.annot转化到MNI152空间?

大多数神经影像学研究的结果是在体积(例如,MNI152)或表面(例如,fsaverage)坐标系中报告的。体积坐标系和表面坐标系之间的准确映射可以促进许多应用,例如将 fMRI 组分析从 MNI152 投影到 fsaverage 以进行可视化或从 fsaverage 投影静息状态 fMRI 分区对 MNI152进行新数据的体积分析。Wu et al., 2018 评估了三种在 MNI152通过模拟上述应用的 Colin27 和 fsaverage 坐标系:将组平均数据从 MNI152/Colin27 投影到

从事脑科学核磁共振方法学研究,在Nature communications等权威期刊发表研究论文,熟练掌握磁共振处理方法和统计学方法,欢迎大家和我交流。 2975

LeetCode OJ:Max Points on a Line

Max Points on a Line  Total Accepted: 3110 Total Submissions: 31616My Submissions Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

王川的私房菜 1056

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Leetcode: Max Points on a Line 分析理解

题目大意:给定一系列二维空间内的点,找出zh

Amy的学习之路 813

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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. 居然是图像处理那个求直线算法的原理的简化版! /** * Definition for a point. * struct Point { * int x; *

博客已退休 408

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

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. 解题思路: 1,在所有点中选定一个点作为中心点,然后再求剩下的点到该中心点的斜率,如果斜率相同的点表示在同一直线上 2,如果剩下点中有与中心点相同的点,则记下相同点的个数,然...

diehuojiang5959的博客 166

leetcode 149: Max Points on a Line

I couldn't think of a specific way to solve the problem until I learned the idea from others. First, choose the point one by one and make it the start point. Then, choose another point after that poin

mikefan1991的博客 238

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 same straight line. SOLUTION 1: 全部的点扫一次,然后计算每一个点与其它点之间的斜率。 创建一个MAP, KEY-VALUE是 斜率:线上的点的数目。 另...

weixin_34174322的博客 188

牛客网LeetCode算法题详解:max-points-on-a-line

欢迎访问我的个人博客(点击进入) max-points-on-a-line 1.题干: 对于给定的n个位于同一二维平面上的点,求最多能有多少个点位于同一直线上 Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 给出的数据结构: /** * Defi...

weixin_43265151的博客 268

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. 分析: 题目是给定一个二维平面上的点,希望求得这个平面上经过最多点的直线经过了多少个点。解这个题目需要用到穷举的思想,即计算每个点到其他点的斜 率,如果斜率相同,则共线

以梦为马,不负韶华 741
上一篇: LeetCode: Maximum Product Subarray
下一篇: LeetCode: Copy List with Random Pointer
rreference
博客等级 码龄12年 0粉丝 26原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值