[LeetCode 第5题] -- Max Points on a Line

简介: 题目链接: Max Points On a Line题目意思: 在二维平面上,有n个点,求同一条直线上最多点的个数题目分析: 1. O(n^2)直接暴力求每个点所在所有直线的最大值,利用map来映射                2.


题目链接: Max Points On a Line

题目意思: 在二维平面上,有n个点,求同一条直线上最多点的个数

题目分析: 1. O(n^2)直接暴力求每个点所在所有直线的最大值,利用map来映射

                2. 有个wa点有可能重复出现,比如(1,1) 和 (1,1)应该算两个


代码:

/**
 * 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 {
public:
     bool isEqual(Point &a, Point& b);
     int getMaxPoints(map<double, int> &mp);
     int maxPoints(vector<Point> &points);
};

bool Solution::isEqual(Point &a, Point& b) {
     if (a.x == b.x && a.y == b.y) {
	return true; 
     }
     return false;
}
int Solution::getMaxPoints(map<double, int> &mp) { 
     int maxNum = 0; 
     for (map<double, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
	  maxNum = max(maxNum, it->second);
     }
     return maxNum;
}
int Solution::maxPoints(vector<Point> &points) {
     int ansMaxPoints = 0; 
     int numPoints = points.size();
     if (numPoints <= 2) {
 	return numPoints;
     }
     map<double, int> mp;
     const double MAXN = 1.0* (1 << 30);
     const double MIN = 0.0;
     for (int i = 0; i < numPoints; ++i) {
	 mp.clear();
	 int samePoints = 1;
	 for(int j = 0; j < numPoints; ++j){
	     if (i == j) {
		continue;
	     }
	     if (isEqual(points[i], points[j])) {
		samePoints++;
		continue;
	     }
	     double key = 0.0;
	     if (points[i].x == points[j].x) {
	        key = MAXN;
	     }
	     else if (points[i].y == points[j].y) {
		key = MIN;
	     }
	     else {
	        key = 1.0*(points[i].y-points[j].y)/(points[i].x-points[j].x);
	     }
	     mp[key]++;
	}
	ansMaxPoints = max(ansMaxPoints, getMaxPoints(mp)+samePoints);
     }
     return ansMaxPoints;
}

目录
打赏
0
0
0
0
15
分享
相关文章
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 个点,求最多有多少个点在同一条直线上。
112 0
LeetCode 149. Max Points on a Line
LeetCode之Max Consecutive Ones
LeetCode之Max Consecutive Ones
143 0
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
[LeetCode]Max Area of Island 岛屿的最大面积
链接:https://leetcode.com/problems/max-area-of-island/description/难度:Easy题目:695.
885 0
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
7月前
|
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
79 6
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
152 2
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等