Участник:F.Nikitin/MaxPointsOnLine

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/max-points-on-a-line/

from fractions import Fraction
class Solution:
    def maxPoints(self, points):
        if len(points) <= 2:
            return len(points)
 
        m = 0
        for i, p_i in enumerate(points):
            l = {}
            dup = 1
            for j, p_j in enumerate(points):
                if p_i[0] == p_j[0] and p_i[1] == p_j[1] and i != j:  
                    dup += 1
                elif i != j :
                    if p_i[0] == p_j[0]:
                        l['v'] = l.get('v',0) + 1
                    elif p_i[1] == p_j[1]:
                        l['h'] = l.get('h',0) + 1
                    else: 
                        slope = Fraction((p_i[1] - p_j[1]), (p_i[0] - p_j[0]))
                        l[slope] = l.get(slope, 0) + 1
            if len(l) > 0: 
                m = max(m, max(l.values()) + dup)
            else:
                m = max(m, dup)
        return m