-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterviewbit-hashing-points-on-the-straight-line.py
55 lines (49 loc) · 2.45 KB
/
interviewbit-hashing-points-on-the-straight-line.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
44
45
46
47
48
49
50
51
52
53
54
55
from fractions import gcd
class Solution:
# @param A : list of integers
# @param B : list of integers
# @return an integer
def maxPoints(self, A, B):
from fractions import gcd
class Solution:
# @param A : list of integers
# @param B : list of integers
# @return an integer
def maxPoints(self, A, B):
if len(A) <= 2:
return len(A)
points = (zip(A, B))
lines = {}
maximum = 2
for i in range(len(points) - 1):
for j in range(i + 1, len(points)):
num = points[j][1] - points[i][1]
denom = points[j][0] - points[i][0]
if denom == 0 and num == 0:
# points that have identical coordinates form their own "line"
# in addition to any other lines they may be in
intercept = points[i]
elif denom == 0:
# vertical lines
num = 0
intercept = points[j][0]
elif num == 0:
# horizontal lines
num = denom = None
intercept = points[j][1]
else:
# simplify the slope -- I thought it was easier to debug this way than just dividing it
the_gcd = gcd(num, denom)
num /= the_gcd
denom /= the_gcd
# and find the b of the y = mx + b
intercept = points[i][1] - ((num * 1.0) / denom) * points[i][0]
if (num, denom, intercept) in lines:
for point in [(points[i][0], points[i][1], i), (points[j][0], points[j][1], j)]:
if point not in lines[(num, denom, intercept)]:
lines[(num, denom, intercept)].append(point)
else:
lines[(num, denom, intercept)] = [(points[i][0], points[i][1], i),
(points[j][0], points[j][1], j)]
maximum = max(maximum, len(lines[(num, denom, intercept)]))
return maximum