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]]
相关推荐
mmmmath_318 分钟前
LeetCode.020.有效的括号
数据结构·算法·leetcode
Doubbbbbbble云39 分钟前
算法优化中的数学建模与理论界限分析4
算法·数学建模
垆边人似月.42 分钟前
华为机考题:字符个数统计
数据结构·算法·华为
en.en..1 小时前
Linux 进程退出exit(函数)
java·开发语言·算法
All for pursuit.1 小时前
【回溯-2】22.括号生成
数据结构·c++·算法·leetcode
longlongzihan1 小时前
LeetCode 1004.最大连续1的个数III 滑动窗口及动态规划题解
c++·算法·leetcode·dp·滑动窗口
朝朝辞暮i2 小时前
C++ 第 22 课:const —— 不允许随便修改的数据
开发语言·c++·算法
Chen—LSN2 小时前
数据结构——拿捏复杂度
java·c语言·开发语言·数据结构·经验分享·笔记·算法
Chen_harmony2 小时前
六、排序详解
c语言·数据结构·算法·排序算法
-dzk-2 小时前
【贪心算法】LC 55.跳跃游戏
算法·游戏·贪心算法