当前位置:网站首页>149. 直线上最多的点数-并查集做法
149. 直线上最多的点数-并查集做法
2022-08-03 19:36:00 【Mr Gao】
给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。
示例 1:
输入:points = [[1,1],[2,2],[3,3]]
输出:3

示例 2:
输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
输出:4

博主也是第一次用并查集这个方法,觉得确实很神奇,这个方法应该可以解决很多很难解决的问题,但是使用起来需要我们多做一些应用:
解题代码如下:
int find(int x,int *p){
while(x!=p[x]){
x=p[x];
}
return x;
}
int maxPoints(int** points, int pointsSize, int* pointsColSize){
int p[pointsSize];
int i,j;
int max=0;
int cei[pointsSize][2];
for(i=0;i<pointsSize;i++){
printf("||");
int cur=0;
for(j=i+1;j<pointsSize;j++){
cei[cur][0]=points[i][0]-points[j][0];
cei[cur][1]=points[i][1]-points[j][1];
cur++;
}
// printf("cur %d |",cur);
int hash[pointsSize];
for(j=0;j<cur;j++){
p[j]=j;
}
for(j=0;j<cur;j++){
hash[j]=0;
for(int k=j+1;k<cur;k++){
if(cei[k][0]*cei[j][1]-cei[k][1]*cei[j][0]==0){
p[k]=j;
}
}
}
for(j=0;j<cur;j++){
max=fmax(max,++hash[find(j,p)]);
// printf("%d %d |",find(j,p),hash[find(j,p)]);
}
}
printf("max %d ",max);
return max+1;
}
边栏推荐
- Unity获取canvas 下ui 在屏幕中的实际坐标
- LeetCode 622. Designing Circular Queues
- ADS 2023 Download Link
- Teach you to locate online MySQL slow query problem hand by hand, package teaching package meeting
- List类的超详细解析!(超2w+字)
- Postgresql snapshot optimization Globalvis new system analysis (performance greatly enhanced)
- 那些年我写过的语言
- 余弦距离介绍
- 傅里叶变换(深入浅出)
- 标准C语言学习总结11
猜你喜欢
随机推荐
JS 内置构造函数 扩展 prototype 继承 借用构造函数 组合式 原型式creat 寄生式 寄生组合式 call apply instanceof
MySQL超详细安装教程 手把手教你安装MySQL到使用MySQL 最简单的MySQL安装方式,这种方式装,卸载也简单
力扣刷题之求两数之和
基于DMS的数仓智能运维服务,知多少?
微信小程序分享功能
Kettle 读取 Excel 数据输出到 Oracle 详解
阿里巴巴政委体系-第七章、阿里政委培育
ADS 2023 下载链接
LeetCode 952. 按公因数计算最大组件大小
Handler 源码解析
阿里巴巴政委体系-第六章、阿里政委体系运作
CS免杀姿势
MySQL master-slave, 6 minutes you master!
Execute the mysql script file in the docker mysql container and solve the garbled characters
力扣刷题之有效的正方形(每日一题7/29)
Postgresql-xl全局快照与GTM代码走读(支线)
Network protocol-TCP, UDP difference and TCP three-way handshake, four wave
net-snmp私有mib动态加载到snmpd
Compose原理-compose中是如何实现事件分法的
「游戏建模干货」建模大师几步操作,学习经典,赶紧脑补一下吧









