【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的点的线上最大的点数。

找出每一个点的最大连线通过的点数。 其中最大的就是在同一条直线上最大的点的数量。

注意可能有相同的点,注意斜率可能为无穷大。

int maxPoints(vector<Point> &points) {
    int result = 0;
    for(int i = 0; i < points.size(); i++){
        int samePoint = 1;
        unordered_map<double, int> map;
        for(int j = i + 1; j < points.size(); j++){
            if(points[i].x == points[j].x && points[i].y == points[j].y){
                samePoint++;
            }
            else if(points[i].x == points[j].x){
                map[INT_MAX]++;
            }
            else{
                double slope = double(points[i].y - points[j].y) / double(points[i].x - points[j].x);
                map[slope]++;
            }
        }
        int localMax = 0;
        for(auto it = map.begin(); it != map.end(); it++){
            localMax = max(localMax, it->second);
        }
        localMax += samePoint;
        result = max(result, localMax);
    }
    return result;
}

First, let‘s talk about mathematics.

How to determine if three points are on the same line?

The answer is to see if slopes of arbitrary two pairs are the same.

Second, let‘s see what the minimum time complexity can be.

Definitely, O(n^2). It‘s because you have to calculate all slopes between any two points.

Then let‘s go back to the solution of this problem.

In order to make this discussion simpler, let‘s pick a random point A as an example.

Given point A, we need to calculate all slopes between A and other points. There will be three cases:

  1. Some other point is the same as point A.
  2. Some other point has the same x coordinate as point A, which will result to a positive infinite slope.
  3. General case. We can calculate slope.

We can store all slopes in a hash table. And we find which slope shows up mostly. Then add the number of same points to it. Then we know the maximum number of points on the same line for point A.

We can do the same thing to point B, point C...

Finally, just return the maximum result among point A, point B, point C...

时间: 2024-10-02 03:59:31

【leetcode】Max Points on a Line(hard)☆的相关文章

【leetcode】Max Points on a Line (python)

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

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

【leetcode】Reverse Words in a String (python)

陆陆续续几个月下来,终于把题刷完了,过程中遇到的python的题解很少,这里重新用python实现下,所以题解可能都是总结性的,或者是新的心得,不会仅针对题目本身说的太详细. def reverseWords(self, s): s = ' '.join(s.split()[::-1]) return s [ : :  -1 ] 是将元素进行翻转 [leetcode]Reverse Words in a String (python),布布扣,bubuko.com

【leetcode】:Evaluate Reverse Polish Notation (python)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的.然后进行相应的运算,将结果push进栈中. 这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题.这种不同表现在两个操作数符号不同时的情况. 在c 中 3 / -5 = 0,但是在python中, 结果却为 - 1.这种

【leetcode】Pascal&#39;s Triangle I &amp; II (middle)

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 思路:杨辉三角,直接按规律生成即可 vector<vector<int> > generate(int numRows) { vector<vector<int>>

Max Points on a Line(6)

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 如何判断点在同一条直线上呢?可以这么办.设定一个初始起点,计算剩余点到起始点直线的斜率,如果存在斜率相同的情况,则说明这几个点在同一条直线上.但是,还要考虑斜率不存在和重复点的情况.所以单独记录重复点的个数和斜率为正无穷的情况.最后取最大值应该是怎么个取法呢?应该是取 斜率相同的点数+重复点数  和

【leetcode】Median of Two Sorted Arrays(hard)★!!

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 思路: 难,知道用分治算法,却不知道怎么用.只好看答案. 基本的思路是如果中位数是第K个数,A[i]如果是中位数,那么A[i]已经大于了i个数,还应大于K - i - 1个

【leetcode】Binary Tree Right Side View(middle)

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3, 4].

【leetcode】Bitwise AND of Numbers Range(middle)

Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. 思路: 先找前面二进制相同的位,后面不相同的位相与一定为0. 比如: 1111001 1110011 从0011 - 1001 中间一定会经