[LintCode] 最多有多少个点在一条直线上

 1 /**
 2  * Definition for a point.
 3  * struct Point {
 4  *     int x;
 5  *     int y;
 6  *     Point() : x(0), y(0) {}
 7  *     Point(int a, int b) : x(a), y(b) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     /**
13      * @param points an array of point
14      * @return an integer
15      */
16     int maxPoints(vector<Point>& points) {
17         // Write your code here
18         unordered_map<float, int> slopes;
19         int maxp = 0, n = points.size();
20         for (int i = 0; i < n; i++) {
21             slopes.clear();
22             int duplicate = 1;
23             for (int j = i + 1; j < n; j++) {
24                 if (points[i].x == points[j].x && points[i].y == points[j].y) {
25                     duplicate++;
26                     continue;
27                 }
28                 float slope = (points[i].x == points[j].x) ? INT_MAX:
29                               (float)(points[i].y - points[j].y) / (points[i].x - points[j].x);
30                 slopes[slope]++;
31             }
32             maxp = max(maxp, duplicate);
33             for (auto slope : slopes)
34                 if (slope.second + duplicate > maxp)
35                     maxp = slope.second + duplicate;
36         }
37         return maxp;
38     }
39 };
时间: 2024-08-17 11:45:25

[LintCode] 最多有多少个点在一条直线上的相关文章

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

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

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

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

Lining Up(在一条直线上的最大点数目,暴力)

Lining Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1274    Accepted Submission(s): 366 Problem Description ``How am I ever going to solve this problem?" said the pilot.  Indeed, the pilo

objectarx之判断三点是否在一条直线上

bool CCommonFuntion::IsOnLine(AcGePoint2d& pt1, AcGePoint2d& pt2, AcGePoint2d& pt3){ AcGeVector3d vec1 = AcGeVector3d(pt2.x - pt1.x, pt2.y - pt1.y, 0); AcGeVector3d vec2 = AcGeVector3d(pt3.x - pt1.x, pt3.y - pt1.y, 0); double pi = 3.14159265;/

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

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

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 {

判断两条直线的位置关系 POJ 1269 Intersecting Lines

两条直线可能有三种关系:1.共线     2.平行(不包括共线)    3.相交. 那给定两条直线怎么判断他们的位置关系呢.还是用到向量的叉积 例题:POJ 1269 题意:这道题是给定四个点p1, p2, p3, p4,直线L1,L2分别穿过前两个和后两个点.来判断直线L1和L2的关系 这三种关系一个一个来看: 1. 共线. 如果两条直线共线的话,那么另外一条直线上的点一定在这一条直线上.所以p3在p1p2上,所以用get_direction(p1, p2, p3)来判断p3相对于p1p2的关