[LintCode 186] 最多有多少个点在一条直线上(Python)
2017-09-05 14:19
411 查看
题目描述
给出二维平面上的n个点,求最多有多少点在同一条直线上。样例
给出4个点:(1, 2), (3, 6), (0, 0), (1, 3)。
思路
借助辅助二维数组,存储每两点之间的斜率和截距。代码
# Definition for a point. # class Point: # def __init__(self, a=0, b=0): # self.x = a # self.y = b class Solution: # @param {int[]} points an array of point # @return {int} an integer def maxPoints(self, points): # Write your code here if points is None or len(points) == 0: return 0 if len(points) == 1: return 1 tmp = [[None for col in range(len(points))] for raw in range(len(points))] for i in range(len(points)): for j in range(len(points)): if i != j: if points[i].y - points[j].y != 0: k = float(points[i].x - points[j].x)/float(points[i].y - points[j].y) b = points[i].y - k * points[i].x tmp[i][j] = (k, b) elif points[i].y == points[j].y and points[i].x == points[j].x: tmp[i][j] = (0, 0) res = 2 for raw in tmp: t = 0 for i in set(raw): if i is not None: t = max(t, raw.count(i)) t += 1 res = max(res, t) return res
复杂度分析
时间复杂度O(n2),空间复杂度O(n2)。相关文章推荐
- lintcode(186)最多有多少个点在一条直线上
- lintcode-186-最多有多少个点在一条直线上
- LintCode 最多有多少个点在一条直线上
- lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
- [LintCode] 最多有多少个点在一条直线上
- LintCode:最多有多少个点在一条直线上
- lintcode最多有多少个点在一条直线上
- 最多有多少个点在一条直线上-LintCode
- 【LintCode】最多有多少个点在一条直线上
- lintcode,最多有多少个点在一条直线上
- lintcode-有多少个点在同一条直线上-186
- 最多有多少个点在同一条直线上
- 每天一道LeetCode-----平面上n个点,计算最多有多少个点在一条直线上
- Lint_Code_最多有多少个点在一条直线上
- 最多有多少个点在一条直线上
- 解题报告:最多有多少个点在一条直线上
- [leetcode] Max Points on a Line 判断最多有多少个点在同一条直线上
- LinkCode-最多有多少个点在一条直线上
- 网易互娱笔试题----最多有多少点在一条直线上
- LeetCode | 149. Max Points on a Line求多个点里面在一条直线上的点最多有多少个难题