[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.解题思路 若两个点共线,则有如下情况:两个点重合两个点的x坐标相同(斜率无穷大)两个点的斜率相同 y1 = k * x1 + b; y2 = k * x2

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

  • 解题思路
    若两个点共线,则有如下情况:

    • 两个点重合
    • 两个点的x坐标相同(斜率无穷大)
    • 两个点的斜率相同
      y1 = k * x1 + b;
      y2 = k * x2 + b;
      则有k = (y1 - y2) / (x1 - x2);
  • 实现代码

/*****************************************************************
    *  @Author   : 楚兴
    *  @Date     : 2015/2/9 20:46
    *  @Status   : Accepted
    *  @Runtime  : 35 ms
******************************************************************/
#include <vector>
#include <unordered_map>
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 maxNum = 0;
        unordered_map<double,int> mymap;
        int copy;  //重复的点个数
        double k;  //斜率
        for (int i = 0; i < points.size(); i++)
        {
            mymap.clear();
            copy = 0;  //坐标相同点的个数
            for (int j = 0; j < points.size(); j++)
            {
                if (j == i)
                {
                    continue;
                }

                if (points[i].x == points[j].x && points[i].y == points[j].y)
                {
                    copy++;  //相同点
                    continue;
                }

                if (points[i].x == points[j].x)
                {
                    mymap[INT_MAX]++; //斜率不存在
                }
                else 
                {
                    k = (double)(points[i].y - points[j].y) / (points[i].x - points[j].x);
                    mymap[k]++;
                }
            }

            if (mymap.size() == 0)
            {
                maxNum = maxNum > copy ? maxNum : copy;
            }
            int temp;
            for (auto it = mymap.cbegin(); it != mymap.cend(); it++)
            {
                temp = (*it).second + copy;
                maxNum = maxNum > temp ? maxNum : temp;
            }
        }

        return maxNum + 1;  //加上自身
    }
};

int main()
{
    Solution s;
    vector<Point> points;
    Point* p1 = new Point(0, 0);
    Point* p2 = new Point(0, 0);
    points.push_back(*p1);
    points.push_back(*p1);
    cout<<s.maxPoints(points)<<endl;
    delete p1;
    delete p2;
    system("pause");
}
目录
相关文章
|
算法
LeetCode 363. Max Sum of Rect No Larger Than K
给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和。
120 0
LeetCode 363. Max Sum of Rect No Larger Than K
LeetCode 149. Max Points on a Line
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。
111 0
LeetCode 149. Max Points on a Line
LeetCode之Max Consecutive Ones
LeetCode之Max Consecutive Ones
143 0
|
Java Python
LeetCode 485:连续最大1的个数 Max Consecutive Ones(python java)
公众号:爱写bug 给定一个二进制数组, 计算其中最大连续1的个数。 Given a binary array, find the maximum number of consecutive 1s in this array. 示例 1: 输入: [1,1,0,1,1,1] 输出: 3 解释: 开头的两位和最后的三位都是连续1,所以最大连续1的个数是 3. 注意: 输入的数组只包含 0 和1。
870 0
|
Java
[LeetCode]Max Area of Island 岛屿的最大面积
链接:https://leetcode.com/problems/max-area-of-island/description/难度:Easy题目:695.
885 0
|
6月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
7月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
78 6
|
7月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
152 2
|
4月前
|
机器学习/深度学习 人工智能 自然语言处理
280页PDF,全方位评估OpenAI o1,Leetcode刷题准确率竟这么高
【10月更文挑战第24天】近年来,OpenAI的o1模型在大型语言模型(LLMs)中脱颖而出,展现出卓越的推理能力和知识整合能力。基于Transformer架构,o1模型采用了链式思维和强化学习等先进技术,显著提升了其在编程竞赛、医学影像报告生成、数学问题解决、自然语言推理和芯片设计等领域的表现。本文将全面评估o1模型的性能及其对AI研究和应用的潜在影响。
107 1