149. Max Points on a Line
这道题的想法还是挺简单的,只是要分清三种情况,点的横坐标相同,点重合,其它情况。
# Definition for a point.
# class Point(object):
# def __init__(self, a=0, b=0):
# self.x = a
# self.y = b
import numpy as np
class Solution(object):
def maxPoints(self, points):
"""
:type points: List[Point]
:rtype: int
"""
if not points:
return 0
if len(points) <= 2:
return len(points)
# 想法很简单就是求每两点间的slope
res = 0
for p in points:
m = {}
duplicate = 1
vertical = 0
for p1 in points:
if p != p1:
if p.x == p1.x and p.y == p1.y:
duplicate += 1
elif p.x == p1.x:
vertical += 1
else:
slope = (p1.y - p.y) * np.longdouble(1) / (p1.x - p.x)
m[slope] = m.get(slope, 0) + 1
print m
res = max(res, vertical+duplicate)
if m:
res = max(res, max(m.values())+duplicate)
return res