• 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
  • 相关阅读:
    资料总结分享:临床重要数据库
    【知识总结】开发提效50%的Javascript常用函数
    端到端自动驾驶模型SparseDrive论文阅读笔记
    redis 的java客户端 基础(一)
    Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
    代码审计——任意文件下载详解(二)
    【期权系列】顶部和底部信号:期权看跌看涨比(PCR)
    探索 SOCKS5 代理在跨境电商中的网络安全应用
    IMU武装智能昆虫
    【Docker中Kafka+Zookeeper基本命令使用】
  • 原文地址:https://blog.csdn.net/navicheung/article/details/134544756