My Python Code - O(n^2)


  • -2
    F

    `# Definition for a point

    class Point:

    def init(self, a=0, b=0):

    self.x = a

    self.y = b

    class Solution:
    # @param points, a list of Points
    # @return an integer
    def maxPoints(self, points):
    res = [0 for i in range(len(points)-1)]
    n = len(points)
    if n <= 2:
    return n
    for i in range(len(points)-1):
    maxlength = 0
    same = 0
    slope = {}
    for j in range(i+1, len(points)):
    if points[j].x == points[i].x and points[j].y == points[i].y:
    same += 1
    continue
    elif points[j].y == points[i].y:
    s = 'sameY'
    else:
    s = float((points[j].x - points[i].x)) / float((points[j].y - points[i].y))
    if s in slope:
    slope[s].append(j)
    else:
    slope[s] = [j]
    for s in slope:
    maxlength = max(maxlength, len(slope[s]))
    maxlength += same + 1
    res[i] = maxlength
    return max(res)`


  • 0
    G

    make sure the preview looks nice before posting


  • 0
    C

    It is hard to read your code.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.