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]]
相关推荐
小许同学记录成长34 分钟前
三维重建技术文档
算法·无人机
小O的算法实验室2 小时前
2026年ASOC,基于多目标优化去噪双存档进化算法+路径规划,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
2601_954526752 小时前
逆向解析Temu底层动销算法:基于API高并发轮询与全域存量透视的自动化架构重构
算法·架构·自动化
Σίσυφος19003 小时前
数据标准化(拟合的时候使用非常重要)
人工智能·算法
knight_9___3 小时前
大模型project面试7
人工智能·python·算法·面试·大模型·agent
NashSKY4 小时前
EM 算法完整推导与本质剖析
算法·机器学习·概率论
foundbug9994 小时前
MATLAB实现:基于图像对比度和波段相关性的高光谱波段选择算法
开发语言·算法·matlab
嘿嘿嘿x34 小时前
Linux-实践
linux·运维·算法
Godspeed Zhao5 小时前
从零开始学AI14——最大似然估计与对数损失函数
算法·逻辑回归·最大似然