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]]
相关推荐
一去不复返的通信er4 小时前
力扣-图论-岛屿数量
算法·leetcode·图论
程序 代码狂人7 小时前
切片表与分区表的区别(数据开发面试版)
java·大数据·开发语言·数据仓库·算法
HDU-Dade8 小时前
【校招笔试真题】简要介绍 Self-Attention 的前沿算法
人工智能·算法
Xin7709 小时前
LeetCode 79. 单词搜索(DFS 回溯法)
leetcode
小郎君65810 小时前
Vue keep-alive 切标签后滚动位置回顶?因为 onDeactivated 里读到的 scrollTop 是 0
算法
邵奈一10 小时前
GPT-6.1 Terra速度是Sol的3倍,价格减半
算法
RaaS10010 小时前
Node.js Brotli RCE 漏洞自查指南:fetch、undici、axios 都在影响面,三分钟确认你的版本
人工智能·深度学习·算法
shehuiyuelaiyuehao11 小时前
算法56,重排链表
数据结构·算法·链表
qq_4523962311 小时前
第四篇:《索引原理与实战:B-tree、Hash、GIN、GiST、BRIN》
算法·哈希算法·gin