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) : x(a), y(b) {}
}
class Solution {
public:
    int maxPoints(vector< Point > &points) {
        float k;
        int counter = 0,ret = 0,tmpMax = 0;
        unordered_map< float,int > slopes;
        for(int i = 0; i < points.size(); i++){//按点遍历,那么计算斜率就能确定一条直线,因为已有一个点的信息,
            for(int j = 0; j < points.size(); j++){
                if(points[j].x==points[i].x&&points[j].y==points[i].y){//重合的点
                    counter++;
                    continue;
                }
                k = (float)(points[j].y-points[i].y)/(points[j].x-points[i].x);
                slopes[k]++;
            }
            for(unordered_map< float,int >::iterator it=slopes.begin(); it!=slopes.end();it++){
                tmpMax =max(tmpMax, it->second);
            }
            ret = max(tmpMax+counter, ret);
            counter = 0;
            tmpMax = 0;
            slopes.clear();
        }
        return ret;
    }
};
时间: 2024-10-10 10:13:00

149. Max Points on a Line (Array; Greedy)的相关文章

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

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();

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每日一题】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)可能存

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

[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

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 {

【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的情况,此种情况下点也在同一直线下,所以需要单独做判断. 还有一种情况就是如果点坐标一致,则不管其他点

LeetCode: Max Points on a Line [149]

[题目] Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. [题意] 给定一堆点,要求找出一条之前上的最大点数 [思路] 没什么好的方法,从每个点P出发,遍历所有的情况 从每个点P出发,斜率相同的点即为统一之前上的点 注意两种特殊情况: 1. 两个点重合(即为同一个点) 2. 两个点的很坐标相同,即两天构成的之前垂直于X轴 [代码] /** * D