leetcode 149. 直线上最多的点数

这题卡了好长时间,心态崩了呀,,,

最开始把斜率当作直线,我真是脑抽,表示一条直线用斜率 k + 截距 b 就可以了。

但是要注意,如果两点x值相等,那么我们斜率为正无穷,为了能表示不同的与x轴垂直直线,用x坐标表示一条与x轴垂直的直线;如果两点y值相等,那么我们的斜率为0,为了表示,则用y值表示不同的直线。

对于一般直线就用k b表示,题中的数据有坑的地方,两点相等也是同一条直线,我们用不同的下标区分坐标相同的点就可以了。

class Solution {
public:
    int maxPoints(vector<vector<int>>& points) {
        if (points.size()==1||points.size()==0) return points.size();
        vector<vector<int> >& p=points;
        map<long double,set<int> > x,y;
        map<pair<long double,long double>,set<int> > mp;
        int sz=points.size();
        for (int i=0;i<sz;i++) {
            for (int j=i+1;j<sz;j++) {
                if (p[i][0]==p[j][0]) {
                    x[p[i][0]].insert(i);
                    x[p[i][0]].insert(j);
                }
                else if (p[i][1]==p[j][1]) {
                    y[p[i][1]].insert(i);
                    y[p[i][1]].insert(j);
                }
                else {
                    long double k=(long double)(p[i][1]-p[j][1])/(long double)(p[i][0]-p[j][0]);
                    long double b=p[i][1]-k*p[i][0];
                    mp[make_pair(k,b)].insert(i);
                    mp[make_pair(k,b)].insert(j);
                }
            }
        }
        int ans=0;
        for (auto p: x) {
            int tmp=p.second.size();ans=max(ans,tmp);
        }
        for (auto p: y) {
            int tmp=p.second.size();ans=max(ans,tmp);
        }
        for (auto p: mp) {
            int tmp=p.second.size();ans=max(ans,tmp);
        }
        return ans;
    }
};

  

 

  

原文地址:https://www.cnblogs.com/xyqxyq/p/12234018.html

时间: 2024-08-30 01:42:16

leetcode 149. 直线上最多的点数的相关文章

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 {

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 +------------------

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

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

3403: [Usaco2009 Open]Cow Line 直线上的牛

3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 71  Solved: 62[Submit][Status] Description 题目描述 约翰的N只奶牛(编为1到N号)正在直线上排队.直线上开始的时候一只牛也没有.接下来发生了S(1≤S≤100000)次事件,一次事件可能是以下四种情况之一: .一只奶牛加入队伍的左边(输入“AL”). .一只奶牛加入队伍的右边(输入“AR

一条直线上N个线段所覆盖的总长度

原文:http://blog.csdn.net/bxyill/article/details/8962832 问题描述: 现有一直线,从原点到无穷大. 这条直线上有N个线段.线段可能相交. 问,N个线段总共覆盖了多长?(重复覆盖的地区只计算一次) ================================================ 解题思路: 可以将每个线段拆分成“单位1” 遍历所有线段,使用一个数组记录每个线段所走过的“单位1” 最后统计数组中被走过的中“单位1”的个数,即是所有线

BZOJ 3403: [Usaco2009 Open]Cow Line 直线上的牛( deque )

直接用STL的的deque就好了... ---------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<deque> #define rep( i , n ) for( int i = 0 ; i <

已知直线上两点求直线的一般式方程

一般式方程在计算机领域的重要性 常用的直线方程有一般式 点斜式 截距式 斜截式 两点式等等.除了一般式方程,它们要么不能支持所有情况下的直线(比如跟坐标轴垂直或者平行),要么不能支持所有情况下的点(比如x坐标相等,或者y坐标相等).所以一般式方程在用计算机处理二维图形数据时特别有用. 已知直线上两点求直线的一般式方程 已知直线上的两点P1(X1,Y1) P2(X2,Y2), P1 P2两点不重合.则直线的一般式方程AX+BY+C=0中,A B C分别等于: A = Y2 - Y1 B = X1

BZOJ3403: [Usaco2009 Open]Cow Line 直线上的牛

3403: [Usaco2009 Open]Cow Line 直线上的牛 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 48  Solved: 41[Submit][Status] Description 题目描述 约翰的N只奶牛(编为1到N号)正在直线上排队.直线上开始的时候一只牛也没有.接下来发生了S(1≤S≤100000)次事件,一次事件可能是以下四种情况之一: .一只奶牛加入队伍的左边(输入“AL”). .一只奶牛加入队伍的右边(输入“AR