149 Max Points on a Line 直线上最多的点数

给定二维平面上有 n 个点,求最多有多少点在同一条直线上。

详见:https://leetcode.com/problems/max-points-on-a-line/description/

    /**
     * Definition for a point.
     * struct Point {
     *     int x;
     *     int y;
     *     Point() : x(0), y(0) {}
     *     Point(int a, int b) : x(a), y(b) {}
     * };
     */
class Solution {
public:
    int maxPoints(vector<Point>& points) {
        int res = 0;
        for (int i = 0; i < points.size(); ++i)
        {
            map<pair<int, int>, int> m;
            int duplicate = 1;
            for (int j = i + 1; j < points.size(); ++j)
            {
                if (points[i].x == points[j].x && points[i].y == points[j].y)
                {
                    ++duplicate;
                    continue;
                }
                int dx = points[j].x - points[i].x;
                int dy = points[j].y - points[i].y;
                int d = gcd(dx, dy);
                ++m[{dx / d, dy / d}];
            }
            res = max(res, duplicate);
            for (auto it = m.begin(); it != m.end(); ++it)
            {
                res = max(res, it->second + duplicate);
            }
        }
        return res;
    }
    int gcd(int a, int b)
    {
        return (b == 0) ? a : gcd(b, a % b);
    }
};

参考:https://www.cnblogs.com/grandyang/p/4579693.html

原文地址:https://www.cnblogs.com/xidian2014/p/8727564.html

时间: 2024-10-12 04:27:42

149 Max Points on a Line 直线上最多的点数的相关文章

[Swift]LeetCode149. 直线上最多的点数 | Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example 1: Input: [[1,1],[2,2],[3,3]] Output: 3 Explanation: ^ | |        o |     o |  o   +-------------> 0  1  2  3 4 Example 2: Input: [[1,1],[3,2]

Max Points on a Line(直线上最多的点数)

给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | |        o |     o |  o   +-------------> 0  1  2  3 4 示例 2: 输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 输出: 4 解释: ^ | | o |     o   o |      o |  o   o +------------------

149. Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 要求是输入n个坐标点,输出最多有多少个点在同一条直线上. 代码提交如下: 1 int Solution::maxPoints(vector<Point>& points) 2 { 3 if (points.size()<=2) 4 { 5 return points.size();

【leetcode每日一题】149.Max Points on a line

题目:Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 解析:题目要求是给定n个2维的点,求出总共有多少个点在同一条直线上.由数学知识可知,给定三个点a,b,c,如果三个点在一条直线上,则a和b的斜率与c和d的斜率是相同的.用哈希表来做,针对每个点,求出其余所有点与它的斜率,找出最大值.然后遍历所有的点,返回最大值. 特别需要主意的几点:1)可能存

[leedcode 149] Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. /** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */ public clas

【leetcode】149 Max Points on a Line

题目说明 https://leetcode-cn.com/problems/max-points-on-a-line/description/ 给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 解法1 首先遍历点,在第二层循环中使用map记录倾角斜率及其匹配次数. 斜率dx/dy(dx表示两点x轴的距离,dy表示两点y轴的差值)进行计算,但是这里有一种特殊情况,就是dy=0的情况,此种情况下点也在同一直线下,所以需要单独做判断. 还有一种情况就是如果点坐标一致,则不管其他点

UVA Lining Up (一条直线上最多的点数)

Description  Lining Up  ``How am I ever going to solve this problem?" said the pilot. Indeed, the pilot was not facing an easy task. She had to drop packages at specific points scattered in a dangerous area. Furthermore, the pilot could only fly over

149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. /** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */ class Solutio

149. Max Points on a Line (Array; Greedy)

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 思路:对于某一点来说,在经过该点的直线中选取节点数量最多的直线:对于全局来说,必定是某个局部点满足条件的直线之一=>局部最优解也是全局最优解=>贪心法. struct Point { int x; int y; Point() : x(0), y(0) {} Point(int a, int b)