[LeetCode] Max Points on a Line, Solution
2016-01-12 11:12
323 查看
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
[Thoughts]
任意一条直线都可以表述为
y = ax + b
假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有
y1 = kx1 +b
y2 = kx2 +b
由此可以得到关系,k = (y2-y1)/(x2-x1)。即如果点c和点a的斜率为k, 而点b和点a的斜率也为k,那么由传递性,可以知道点c和点b也在一条线上。解法就从这里来
取定一个点(xk,yk), 遍历所有节点(xi, yi), 然后统计斜率相同的点数,并求取最大值即可
[code]1: int maxPoints(vector<Point> &points) { 2: unordered_map<float, int> statistic; 3: int maxNum = 0; 4: for (int i = 0; i< points.size(); i++) 5: { 6: statistic.clear(); 7: statistic[INT_MIN] = 0; // for processing duplicate point 8: int duplicate = 1; 9: for (int j = 0; j<points.size(); j++) 10: { 11: if (j == i) continue; 12: if (points[j].x == points[i].x && points[j].y == points[i].y) // count duplicate 13: { 14: duplicate++; 15: continue; 16: } 17: float key = (points[j].x - points[i].x) == 0 ? INT_MAX : 18: (float) (points[j].y - points[i].y) / (points[j].x - points[i].x); 19: statistic[key]++; 20: } 21: for (unordered_map<float, int>::iterator it = statistic.begin(); it != statistic.end(); ++it) 22: { 23: if (it->second + duplicate >maxNum) 24: { 25: maxNum = it->second + duplicate; 26: } 27: } 28: } 29: return maxNum; 30: }
若干注意事项:
1. 垂直曲线, 即斜率无穷大
2. 重复节点。
[Thoughts]
任意一条直线都可以表述为
y = ax + b
假设,有两个点(x1,y1), (x2,y2),如果他们都在这条直线上则有
y1 = kx1 +b
y2 = kx2 +b
由此可以得到关系,k = (y2-y1)/(x2-x1)。即如果点c和点a的斜率为k, 而点b和点a的斜率也为k,那么由传递性,可以知道点c和点b也在一条线上。解法就从这里来
取定一个点(xk,yk), 遍历所有节点(xi, yi), 然后统计斜率相同的点数,并求取最大值即可
[code]1: int maxPoints(vector<Point> &points) { 2: unordered_map<float, int> statistic; 3: int maxNum = 0; 4: for (int i = 0; i< points.size(); i++) 5: { 6: statistic.clear(); 7: statistic[INT_MIN] = 0; // for processing duplicate point 8: int duplicate = 1; 9: for (int j = 0; j<points.size(); j++) 10: { 11: if (j == i) continue; 12: if (points[j].x == points[i].x && points[j].y == points[i].y) // count duplicate 13: { 14: duplicate++; 15: continue; 16: } 17: float key = (points[j].x - points[i].x) == 0 ? INT_MAX : 18: (float) (points[j].y - points[i].y) / (points[j].x - points[i].x); 19: statistic[key]++; 20: } 21: for (unordered_map<float, int>::iterator it = statistic.begin(); it != statistic.end(); ++it) 22: { 23: if (it->second + duplicate >maxNum) 24: { 25: maxNum = it->second + duplicate; 26: } 27: } 28: } 29: return maxNum; 30: }
若干注意事项:
1. 垂直曲线, 即斜率无穷大
2. 重复节点。
相关文章推荐
- [LeetCode] Sort List, Solution
- [LeetCode] Clone Graph, Solution
- [LeetCode] Evaluate Reverse Polish Notation, Solution
- 缓冲I/O与非缓冲I/O(fopen与open系列函数区别)
- 湖大OJ-实验A---- ADFA的可判定性
- 使用.NET进行高效率互联网敏捷开发的思考和探索【一、概述】
- 数据分页 THINKPHP3.2 分页 三种分页方法
- Qt UI 布局
- Dockerfile使用
- 端口被占用怎么办?
- python中if __name__ == '__main__': 的解析
- Mysql几种索引类型的区别及适用情况
- 非UI线程中Toast
- [LeetCode] Search a 2D Matrix, Solution
- [LeetCode] Remove Duplicates from Sorted List II, Solution
- [LeetCode] Unique Binary Search Trees, Solution
- [LeetCode] Unique Binary Search Trees II, Solution
- [LeetCode] Same Tree, Solution
- [LeetCode] Convert Sorted Array to Binary Search Tree, Solution
- [Interview] Serialize and De-serialize a tree