当前位置:网站首页>Leetcode149 - maximum number of points on a line - Math - hash table
Leetcode149 - maximum number of points on a line - Math - hash table
2022-04-23 14:48:00 【Li Fan, hurry up】
Note:
Enumerate all points , Let him be the origin
After selecting the origin , Find all the points again , There are three situations at this time
1、 The point coincident with the current origin ( It can be regarded as the point above the straight line with arbitrary slope )
2、 A point on a vertical line ( At this time, the slope can't be calculated , The divisor cannot be zero 0)
3、 Normal point , The point that forms a straight line with a slope
Then each point is classified according to the slope , Finally, first check the number of points with normal slope and points on the vertical line
And then look at answer and More points after this traversal + The point of coincidence Who is more
On the second traversal , There must be a coincidence point , Is the current point , This is not a repetition ?
It is only counted during the second traversal , Not much
The code is as follows :
class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
int ans = 0;
for(auto& p: points){
int s = 0, v = 0;
unordered_map<long double, int> cnt;
for(auto& q : points){
if(p == q) s ++;
else if(p[0] == q[0]) v ++;
else{
long double k = (long double) (q[1] - p[1]) / (q[0] - p[0]);
cnt[k] ++;
}
}
int c = v;
for(auto [k, v] : cnt) c = max(c, v);
ans = max(ans, c + s);
}
return ans;
}
};
版权声明
本文为[Li Fan, hurry up]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231447599859.html
边栏推荐
- 1 - first knowledge of go language
- A good tool: aardio
- 8.5 循环神经网络简洁实现
- epoll 的EPOLLONESHOT 事件———实例程序
- 1n5408-asemi rectifier diode
- do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
- SVN详细使用教程
- Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
- 帧同步 实现
- Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
猜你喜欢
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
自动化的艺术
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
3、 Gradient descent solution θ
【工厂模式详解】工厂方法模式
一个月把字节,腾讯,阿里都面了,写点面经总结……
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
外包幹了四年,廢了...
随机推荐
Introduction to Arduino for esp8266 serial port function
Explain TCP's three handshakes in detail
UML project example -- UML diagram description of tiktok
Proteus simulation design of four storey and eight storey elevator control system, 51 single chip microcomputer, with simulation and keil c code
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
eolink 如何助力远程办公
Arduino for esp8266串口功能简介
vscode中文插件不生效问题解决
8.4 循环神经网络从零实现
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
Pnpm installation and use
1-初识Go语言
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
1N5408-ASEMI整流二极管1N5408
8.2 文本预处理
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
外包幹了四年,廢了...
[servlet] detailed explanation of servlet (use + principle)
你还不知道责任链模式的使用场景吗?
编程哲学——自动加载、依赖注入与控制反转