-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path149.py
43 lines (31 loc) · 1.17 KB
/
149.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# https://leetcode.com/problems/max-points-on-a-line/
from typing import List
from collections import defaultdict
import math
class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
if len(points) <= 2:
return len(points)
d = defaultdict(lambda : defaultdict(lambda: []))
for i in range(len(points)):
for o in range(len(points)):
if i != o:
slope = self.slope(points[i], points[o])
d[i][slope].append(o)
m = 0
print(d)
for i in d.keys():
for slope in d[i].keys():
if len(d[i][slope]) + 1 > m:
m = len(d[i][slope]) + 1
return m
def slope(self, a, b):
if a[0] == b[0]:
return math.inf
else:
return (b[1] - a[1]) / (b[0] - a[0])
# print(Solution().maxPoints([[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]))
# print(Solution().maxPoints([[1,1],[2,2],[3,3]]))
# print(Solution().maxPoints([[0,0],[1,1],[0,0]]))
# print(Solution().maxPoints([[1,1],[2,1],[2,2],[1,4],[3,3]]))
print(Solution().maxPoints([[1,1],[1,1],[2,2],[2,2]]))