leetcode-[3]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

思路:最多的点,必然是点连成线时,所有斜率相同的最多的组合情况;

     那么如果不在同一直线点的组合也可能斜率相同,找其中一点与其它点连即可。

#include <iostream>
#include <vector>
#include <map>

using namespace std;

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) {
        if (points.size() < 2)
            return points.size();

        int result = 0;
        for (int i = 0; i < points.size(); i++) {
            map<pair<int, int>, int> line;

            int overlap = 0, vertical = 0, curMax = 0;
            for (int j = i + 1; j < points.size(); j++)    {
                if((points[i].x == points[j].x) &&
                   (points[i].y == points[j].y)) {
                    overlap ++;
                    continue;
                }
                else if (points[i].x == points[j].x) {
                    vertical ++;
                }
                else {
                    int dx = points[i].x - points[j].x;
                    int dy = points[i].y - points[j].y;    

                    int gcd = GCD(dx, dy);

                    dx /= gcd;
                    dy /= gcd;

                    line[make_pair(dx, dy)]++;
                    curMax = max(line[make_pair(dx, dy)], curMax);
                }
                curMax = max(vertical, curMax);
            }
            result = max(result, curMax+overlap+1);
        }
        return result;
    }

    int GCD(int a, int b) {
        if (b == 0)
            return a;
        return GCD(b, a%b);
    }
};

int main() {
    vector<Point> points;

    points.push_back(Point(3, 4));
    points.push_back(Point(3, 4));
    points.push_back(Point(3, 4));
    Solution *solution = new Solution();
    cout << solution->maxPoints(points) << endl;

    // (3,10),(0,2),(0,2),(3,10)
    vector<Point> points2;
    points2.push_back(Point(3, 10));
    points2.push_back(Point(0, 2));
    points2.push_back(Point(0, 2));
    points2.push_back(Point(3, 10));
    cout << solution->maxPoints(points2) << endl;

    return 0;
}
时间: 2024-08-10 02:01:54

leetcode-[3]Max Points on a Line的相关文章

【leetcode】Max Points on a Line (python)

给定一个点,除该点之外的其他所有点中,与该点的关系要么是共线,要么就是共点,也就是两点重合. 共线有三种情况:水平共线,垂直共线,倾斜的共线.合并下这三种情况就是斜率存在的共线和斜率不存在的共线. 那么我们的任务就是针对每个点,找出与其共线的这些情况中,共线最多的点的个数. 注意:最终的结果别忘了加上共点的个数. class Solution: def maxPoints(self, points ): if len( points ) <= 1: return len( points ) ma

[LeetCode OJ] 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.

//定义二维平面上的点struct Point { int x; int y; Point(int a=0, int b=0):x(a),y(b){} }; bool operator==(const Point& left, const Point& right) { return (left.x==right.x && left.y==right.y); } //求两个点连接成的直线所对应的斜率 double line_equation(const Point&

LeetCode OJ - 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. 解题思路: 第一反应:枚举两个点组成的直线,然后看其他的点在不在这条直线上,在此过程中统计最大值.此思路的复杂度 O(n^3) 参考了网上的思路:枚举第一个点,用unordered_map来记录其余的点和这个点的斜率,若斜率相同则代表这些点在同一直线上.避免double问题,把斜率转化成化简

[LeetCode][JavaScript]Max Points on a Line

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. 求有多少点在一直线上. 粗暴地用二重循环遍历. 每一轮都构造一个哈希表,用来记录斜率,斜率k = (y1 - y2) / (x1 - x2). 注意到特殊情况: 1.两点重合,用countSamePoint记下重复的点,最后加到结果上. 2.两点与X轴平行,

LeetCode:Max Points on a line

题目: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. 这道题需要稍微转变一下思路,用斜率来实现,试想找在同一条直线上的点,怎么判断在一条直线上,唯一的方式也只有斜率可以完成,我开始没想到,后来看网友的思路才想到的,下面是简单的实现:其中有一点小技巧,利用map<double, int>来存储具有相同斜率

【LeetCode】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个点中,最多共线的点数. [思路] 比较直观的方法是,三层循环,以任意两点划线,判断第三个点是否在这条直线上. [Java代码] /** * Definition for a point. * class Point { * int x; * int y; * Point()

Java for 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. 解题思路: 本题主要需要考虑到斜线的情况,可以分别计算出过points[i]直线最多含几个点,然后算出最大即可,由于计算points[i]的时候,前面的点都计算过了,所以不需要把前面的点考虑进去,所以问题可以转化为过points[i]的直线最大点的个数,解题思路是用一个HashMap储存斜率,遍历p

[Leetcode][JAVA] 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. 对每个点,考察其他点与它组成的直线斜率,使用HashMap将斜率与点个数对应起来. 需要注意的一点是特殊斜率,比如两点重复,两点横坐标相同.所以用String表示斜率比较好 获取斜率函数: 1 public String getSlope(Point p1, Point p2) 2 { 3 if(p

【leetcode】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. 思路: 自己脑子当机了,总是想着斜率和截距都要相同.但实际上三个点是一条直线的话只要它们的斜率相同就可以了,因为用了相同的参照点,截距一定是相同的. 大神的做法: 对每一个点a, 找出所有其他点跟a的连线斜率,相同为同一条线,记录下通过a的点的线上最大的点数. 找出每一个点的最大连线通过的点数. 其

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. /** * 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