Why my python code has "Runtime Error"?

  • 0
    # Definition for a point
    # class Point:
    #     def __init__(self, a=0, b=0):
    #         self.x = a
    #         self.y = b
    class Line(object):
        def __init__(self, point1, point2):
            self._is_horizontal = False
            self._is_vertical = False
            self.n = None
            self.a = None
            self.calculate(point1, point2)
        def calculate(self, point1, point2):
            if point1.x == point2.x:
                self.n = point1.x
                self._is_vertical = True
            elif point1.y == point2.y:
                self.n = point1.y
                self._is_horizontal = True
                self.n = (point1.y - point2.y)/(point1.x - point2.x)
                self.a = (self.n * (point1.x + point2.x) - (point1.y + point2.y))/ (2 * self.n)
        def isHorizontal(self):
            return self._is_horizontal
        def isVertical(self):
            return self._is_vertical
        def isPointOnLine(self, point):
            if self._is_horizontal:
                return point.y == self.n
            elif self._is_vertical:
                return point.x == self.n
                return self.n*(point.x - self.a) == point.y
        def isSameLine(self, line):
            check_vertical = (self.isVertical() == line.isVertical())
            check_horizontal = (self.isHorizontal() == line.isHorizontal())
            check_n = (self.n == line.n)
            check_a = (self.a == line.a)
            return (check_vertical and check_horizontal and check_n and check_a)
    class Solution:
        # @param points, a list of Points
        # @return an integer
        def maxPoints(self, points):
            line_dict ={}
            while points:
                point = points.pop()
                for another_point in points:
                    line = Line(point, another_point)
                    exist = False
                    for existing_line in line_dict.keys():
                        if line.isSameLine(existing_line):
                            exist = True
                            same_line = existing_line
                    if exist:
                        if not point in line_dict[same_line]:
                        if not another_point in line_dict[same_line]:
                        line_dict[line] = [point, another_point]
            return max([len(item) for item in line_dict.values()])

  • 0

    It seems to run ok on my laptop

  • 0

    Changed the last line to this, and now it's "Timeout":

        numbers = [len(item) for item in line_dict.values()]
        if numbers:
            return max(numbers)
            return 0

Log in to reply

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