当前位置:网站首页>Maximum number of points on the line ----- hash table solution
Maximum number of points on the line ----- hash table solution
2022-06-11 05:21:00 【Food can be very delicious】
Title Description
Give you an array points , among points[i] = [xi, yi] Express X-Y A point on the plane . Find out how many points are in the same line at most .
Example 1:
Input :points = [[1,1],[2,2],[3,3]]
Output :3
Example 2:
Tips :
Input :points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output :4
1 <= points.length <= 300
points[i].length == 2
-104 <= xi, yi <= 104
points All the points in Different from each other
Their thinking
Let's start with a question , What are the conditions on the same line ?
The slope of the straight line formed by each point on the straight line is equal
Let's do a double-layer traversal , The outer layer traverses the entire point array , The group of points after the inner layer traverses the position of the outer point , By finding the formula for the slope , Build a hash table , Record the number of slope occurrences in the table , After inner layer traversal , Find the slope with the most occurrences , This is the number of points with the largest number of lines headed by the outer point position (+1).
Code
Stick the leetcode Pass code
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
answer=0
for i in range(len(points)):
temp_dict=dict()
for j in range(i+1,len(points)):
if (points[j][0]-points[i][0])==0:
temp="*"
else:
temp=(points[j][1]-points[i][1])/(points[j][0]-points[i][0])
if temp not in temp_dict:
temp_dict[temp]=1
else:
temp_dict[temp]+=1
if len(temp_dict)!=0:
b=max(temp_dict.values())
answer=max(b,answer)
return answer+1

边栏推荐
- (15) Infrared communication
- Emnlp2021 𞓜 a small number of data relation extraction papers of deepblueai team were hired
- Opencv learning path (2-5) -- Deep parsing imwrite function
- NVIDIA SMI has failed because it could't communicate with the NVIDIA driver
- 董明珠称“格力手机做得不比苹果差”哪里来的底气?
- Common methods of tool class objectutil
- [NIPS2021]MLP-Mixer: An all-MLP Architecture for Vision
- JVM tuning 6: GC log analysis and constant pool explanation
- New product release: Lianrui launched a dual port 10 Gigabit bypass network card
- 项目-智慧城市
猜你喜欢

自定义View之基础篇

自定义View基础之Layout

(十五)红外通信

Huawei equipment configuration MCE

Using keras to build the basic model yingtailing flower

Analyze while doing experiments -ndk article -jni uses registernatives for explicit method registration

How much current can PCB wiring carry

智能门锁为什么会这么火,米家和智汀的智能门锁怎么样呢?

在未来,机器人或 AI 还有多久才能具备人类的创造力?

Share 𞓜 jointly pre training transformers on unpaired images and text
随机推荐
KD-Tree and LSH
Wxparse parsing iframe playing video
22、生成括号
高斯白噪声(white Gaussian noise,WGN)
About custom comparison methods of classes and custom methods of sort functions
1. use alicloud object OSS (basic)
The programmers of a large factory after 95 were dissatisfied with the department leaders, and were sentenced for deleting the database and running away
wxParse解析iframe播放视频
Tianchi - student test score forecast
Restoration of binary tree -- number restoration
华为设备配置通过GRE隧道接入虚拟专用网
Carrier coordinate system inertial coordinate system world coordinate system
Exhibit express: Lianrui will bring three new products of the industry to debut in visionchina (Shenzhen) 2021
C (I) C basic grammar all in one
How to purchase 25g optical network card
Concurrent search set
Customize the layout of view Foundation
In the future, how long will robots or AI have human creativity?
Poverty has nothing to do with suffering
华为设备配置通过GRE接入虚拟专用网