-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPointsOnLine.py
More file actions
28 lines (23 loc) · 1005 Bytes
/
MaxPointsOnLine.py
File metadata and controls
28 lines (23 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Question link - https://leetcode.com/problems/max-points-on-a-line/description/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
# For each points , we need the a slope for the line
# For no of points on a line , we use hashmap
# Count the points on a line
res = 1
for i in range(len(points)):
p1 = points[i]
# Define the hashmap
count = collections.defaultdict(int)
# Going for the other points
for j in range(i + 1 , len(points)):
p2 = points[j]
# Handle the slope for vertical lines
if p1[0] == p2[0]:
# Set infiniate
slope = float('inf')
else:
slope = (p2[1] - p1[1]) / (p2[0] - p1[0])
count[slope] += 1
res = max(res , count[slope] + 1)
return res