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]]
相关推荐
文丑颜不良啊6 小时前
LeetCode3041~3060题目解析
java·leetcode
罗西的思考6 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
文丑颜不良啊7 小时前
LeetCode3101~3120题目解析
java·leetcode
kyle~11 小时前
模型预测控制(MPC)
算法·机器人·控制·传感器
牢姐与蒯12 小时前
cpp算法之哈希
c++·算法·哈希算法
开开心心就好13 小时前
两张表格数据对比工具,不用写函数也能查
算法·spring·智能手机·pdf·电脑·intellij-idea·音视频
电科_银尘14 小时前
【大模型算法与工程 02】线性回归与逻辑回归:从损失函数到决策边界
算法·逻辑回归·线性回归
玄昌盛不会编程15 小时前
LeetCode——2243. 计算字符串的数字和
java·算法·leetcode
文丑颜不良啊15 小时前
LeetCode3061~3080题目解析
java·leetcode
电科_银尘17 小时前
【大模型算法与工程 01】机器学习的问题定义:监督、无监督、自监督与强化学习
人工智能·算法·机器学习