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]]
相关推荐
米粒136 分钟前
力扣算法刷题 Day 31 (贪心总结)
算法·leetcode·职场和发展
少许极端40 分钟前
算法奇妙屋(四十)-贪心算法学习之路7
java·学习·算法·贪心算法
AlenTech1 小时前
647. 回文子串 - 力扣(LeetCode)
算法·leetcode·职场和发展
py有趣1 小时前
力扣热门100题之合并两个有序链表
算法·leetcode·链表
8Qi81 小时前
LeetCode热题100--45.跳跃游戏 II
java·算法·leetcode·贪心算法·编程
foundbug9992 小时前
基于STM32的步进电机加减速程序设计(梯形加减速算法)
stm32·单片机·算法
北顾笙9802 小时前
day12-数据结构力扣
数据结构·算法·leetcode
凌波粒2 小时前
LeetCode--454.四数相加 II(哈希表)
算法·leetcode·散列表
漫随流水2 小时前
c++编程:D进制的A+B(1022-PAT乙级)
数据结构·c++·算法
tankeven3 小时前
HJ159 没挡住洪水
c++·算法