当前位置:网站首页>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
边栏推荐
- Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
- 全连接层的作用是什么?
- Explanation and example application of the principle of logistic regression in machine learning
- 1-初识Go语言
- 51 MCU flowers, farmland automatic irrigation system development, proteus simulation, schematic diagram and C code
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
- 你還不知道責任鏈模式的使用場景嗎?
- eolink 如何助力遠程辦公
- Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
- LeetCode162-寻找峰值-二分-数组
猜你喜欢

Introduction to Arduino for esp8266 serial port function

利用 MATLAB 编程实现最速下降法求解无约束最优化问题

MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program

ASEMI三相整流桥和单相整流桥的详细对比

一款不错的工具:aardio

Role of asemi rectifier module mdq100-16 in intelligent switching power supply

PCIe X1 插槽的主要用途是什么?

LeetCode165-比较版本号-双指针-字符串

Model location setting in GIS data processing -cesium

capacitance
随机推荐
SVN详细使用教程
LeetCode162-寻找峰值-二分-数组
三、梯度下降求解最小θ
金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
详解TCP的三次握手
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
Parameter stack pressing problem of C language in structure parameter transmission
ASEMI超快恢复二极管与肖特基二极管可以互换吗
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
Advanced application of I / O multiplexing: Processing TCP and UDP services at the same time
go基础 反射
【NLP】HMM隐马尔可夫+维特比分词
Achievements in science and Technology (21)
we引用My97DatePicker 实现时间插件使用
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
科技的成就(二十一)
【JZ46 把数字翻译成字符串】
Swift - Literal,字面量协议,基本数据类型、dictionary/array之间的转换
A blog allows you to learn how to write markdown on vscode
Brute force of DVWA low -- > High
