LeetCode 149. 直线上最多的点数

LeetCode 149. 直线上最多的点数

给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。

示例 1:

输入:points = [[1,1],[2,2],[3,3]]

输出:3

示例 2:

输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]

输出:4

提示:

1 <= points.length <= 300

points[i].length == 2

-104 <= xi, yi <= 104

points 中的所有点 互不相同

恰巧AC,但题解不对,还是看大佬的解法

python 复制代码
class Solution:
    def maxPoints(self, points: List[List[int]]) -> int:
        if len(points) == 1:
            return 1

        combine_mapping = {}
        for i in range(2, len(points) + 1):
            combine_mapping[math.comb(i, 2)] = i
        
        mapping = defaultdict(lambda: 0)
        mapping[(0,0,0)] = 1
        for i in range(len(points)):
            for j in range(i + 1, len(points)):
                (x1, y1), (x2, y2) = points[i], points[j]
                a, b, c = y2 - y1, x1 - x2, x2 * y1 - x1 * y2
                d = a if a else b
                a, b, c = a / d, b / d, c / d
                mapping[(a, b, c)] += 1
        res = (0,0,0)
        for i in mapping:
            if combine_mapping[mapping[res]] <= combine_mapping[mapping[i]]:
                res = i
        return  combine_mapping[mapping[res]]
相关推荐
图灵学术计算机论文辅导17 分钟前
论文推荐|迁移学习+多模态特征融合
论文阅读·人工智能·深度学习·计算机网络·算法·计算机视觉·目标跟踪
threejs源码翻译官1 小时前
显微镜图像处理【优化】- 使用图像风格迁移技术放大图像细节
算法
强德亨上校1 小时前
贪心算法(Greedy Algorithm)详解
算法·贪心算法
浮灯Foden2 小时前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表
西工程小巴3 小时前
实践笔记-VSCode与IDE同步问题解决指南;程序总是进入中断服务程序。
c语言·算法·嵌入式
Tina学编程3 小时前
48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
java·算法
Moonbit3 小时前
MoonBit Perals Vol.06: MoonBit 与 LLVM 共舞 (上):编译前端实现
后端·算法·编程语言
执子手 吹散苍茫茫烟波4 小时前
leetcode415. 字符串相加
java·leetcode·字符串
百度Geek说5 小时前
第一!百度智能云领跑视觉大模型赛道
算法
big_eleven5 小时前
轻松掌握数据结构:二叉树
后端·算法·面试