Skip to content

Commit f594ed8

Browse files
authored
Create 0149-maximum-points-on-a-line.py
1 parent ff32745 commit f594ed8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def maxPoints(self, points: List[List[int]]) -> int:
3+
# 1. For each pt determine if it lies on the longest line
4+
# 2. Count all pts with same slope
5+
# 3. Update result with max
6+
7+
res = 1
8+
for i in range(len(points)):
9+
p1 = points[i]
10+
count = collections.defaultdict(int)
11+
for j in range(i + 1, len(points)):
12+
p2 = points[j]
13+
if p2[0] == p1[0]:
14+
slope = float("inf")
15+
else:
16+
slope = (p2[1] - p1[1]) / (p2[0] - p1[0])
17+
count[slope] += 1
18+
res = max(res, count[slope] + 1)
19+
return res

0 commit comments

Comments
 (0)