码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode //C - 149. Max Points on a Line


    149. Max Points on a Line

    Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
     

    Example 1:

    在这里插入图片描述

    Input: points = [[1,1],[2,2],[3,3]]
    Output: 3

    Example 2:

    在这里插入图片描述

    Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
    Output: 4

    Constraints:

    -1 <= points.length <= 300

    • points[i].length == 2
    • − 1 0 4 < = x i , y i < = 1 0 4 -10^4 <= xi, yi <= 10^4 −104<=xi,yi<=104
    • All the points are unique.

    From: LeetCode
    Link: 149. Max Points on a Line


    Solution:

    Ideas:
    1. Iterate Over Pairs of Points: The outer two loops iterate through all possible pairs of points. Each pair of points (i, j) defines a potential line.

    2. Calculate the Greatest Common Divisor (GCD): For each pair, it calculates the differences in x (dx) and y (dy) coordinates. It then simplifies these differences by dividing them by their greatest common divisor (GCD). This step is crucial because it normalizes the slope of the line. For instance, a slope of 2/4 is the same as 1/2 after reduction by the GCD.

    3. Check Other Points: The innermost loop checks all other points to see if they lie on the line defined by the current pair of points. It does this by using the slope-intercept form of the equation of a line. If another point (k), when inserted into the equation formed by points (i) and (j), satisfies the equation, it means that point (k) is also on the same line.

    4. Count Points on the Same Line: Each time it finds a point that lies on the line, it increments a counter (lineCount). After checking all points, if lineCount is greater than maxPoints, it updates maxPoints.

    5. Return the Maximum: After all pairs have been checked, the function returns the highest value of lineCount, which is the maximum number of points on the same line.

    Code:
    int gcd(int a, int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
    
    int maxPoints(int** points, int pointsSize, int* pointsColSize) {
        if (pointsSize < 3) {
            return pointsSize;
        }
        
        int maxPoints = 2;
        for (int i = 0; i < pointsSize; ++i) {
            for (int j = i + 1; j < pointsSize; ++j) {
                int lineCount = 2;
                int dx = points[j][0] - points[i][0];
                int dy = points[j][1] - points[i][1];
                int d = gcd(dx, dy);
                dx /= d; // Reduce the difference to its simplest form
                dy /= d; // Reduce the difference to its simplest form
                for (int k = 0; k < pointsSize; ++k) {
                    if (k != i && k != j) {
                        if (dx * (points[k][1] - points[i][1]) == dy * (points[k][0] - points[i][0])) {
                            lineCount++;
                        }
                    }
                }
                maxPoints = (lineCount > maxPoints) ? lineCount : maxPoints;
            }
        }
        
        return maxPoints;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
  • 相关阅读:
    首台自主创新全空冷机组在三峡运行,图扑数字孪生机体
    Ubtunu排查磁盘空间是否已满—并清理的方式
    HarmonyOS开发云工程与开发云函数
    【论文笔记】NeRF-RPN: A general framework for object detection in NeRFs
    I.MX6ULL ARM驱动开发---块设备驱动
    微信聚合聊天系统的便捷功能:自动回复
    Michael.W基于Foundry精读Openzeppelin第38期——AccessControlEnumerable.sol
    Excel 数据透视表教程大全之 08 创建计算字段,将销售额除以数量实现计算每种产品单价(教程含数据)
    Eureka:微服务中的服务注册与发现机制
    带你认识一下数仓的分区自动管理
  • 原文地址:https://blog.csdn.net/navicheung/article/details/134544756
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号