LeetCode 149. 直线上最多的点数

LeetCode 149. 直线上最多的点数

给你一个数组 points ,其中 pointsi = 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

pointsi.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]]
相关推荐
O。O蛋黄酥啊39 分钟前
GraphRAG 和 LightRAG 详解与对比
人工智能·python·算法·rag·graphrag·lightrag
Σίσυφος190043 分钟前
depth_from_focus 详解
算法
圣保罗的大教堂1 小时前
leetcode 2996. 大于等于顺序前缀和的最小缺失整数 简单
leetcode
疯狂打码的少年1 小时前
【数据结构】八大排序算法对比总结(时间/空间/稳定性)
数据结构·笔记·算法
树獭哥1 小时前
量化金融入门:从数据规则到随机游走与凯利公式
人工智能·算法·金融·量化金融
圣保罗的大教堂1 小时前
leetcode 3702. 按位异或非零的最长子序列 中等
leetcode
薛定e的猫咪2 小时前
(arXiv 2026)GLiBRL :可学习基函数的深度贝叶斯元强化学习 ----待补充
人工智能·深度学习·学习·算法·机器学习
hetao17338372 小时前
2026-08-21~23 hetao1733837 的刷题记录
c++·算法
青 春 记 忆3 小时前
LeetCode 206. 反转链表|Python 解法详解
python·leetcode·链表