当前位置:网站首页>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
边栏推荐
- 机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
- We reference My97DatePicker to realize the use of time plug-in
- Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
- 详解TCP的三次握手
- Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
- 【Servlet】Servlet 详解(使用+原理)
- [stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
- Outsourcing for four years, abandoned
- MySQL error packet out of order
- 555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
猜你喜欢
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
Introduction to Arduino for esp8266 serial port function
8.4 循环神经网络从零实现
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
GIS数据处理-cesium中模型位置设置
Proteus simulation design of DC adjustable regulated power supply (with simulation + paper and other data)
Don't you know the usage scenario of the responsibility chain model?
随机推荐
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
线程同步、生命周期
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
1 - first knowledge of go language
在游戏世界组建一支AI团队,超参数的多智能体「大乱斗」开赛
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
qt之.pro文件详解
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
Brute force of DVWA low -- > High
Parameter stack pressing problem of C language in structure parameter transmission
we引用My97DatePicker 实现时间插件使用
MySQL error packet out of order
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
Go basic reflection
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
UML project example -- UML diagram description of tiktok
电容
How do I open the win10 startup folder?
Role of asemi rectifier module mdq100-16 in intelligent switching power supply