Leetcode: Max Points on a Line
来源:互联网 发布:阅读题软件 编辑:程序博客网 时间:2024/05/01 00:49
Problem: Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Solution 1: Two points determine a straight line. With n points, we can determine C(n,2) = n*(n-1)/2 straight lines. For each line, traverse all the points to find out whether any point is on the same line (why all points? since two points determined this line, why not only the rest of the points? -- because we also need to count the initial two points; otherwise the count will come short) and count the number of such points. Update the global max on the fly.
Code 1:
/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */public class Solution { public int maxPoints(Point[] points) { if (points == null) { return 0; } if (points.length < 3) { return points.length; } int max = 0; // global max double k = 0; // slope double b = 0; // intercept for (int i = 0; i < points.length - 1; ++i) { for (int j = i + 1; j < points.length; ++j) { int count = 0; // local count boolean vertical = false; if (points[i].x == points[j].x) { // a vertical line vertical = true; } else { k = (double)(points[i].y - points[j].y)/(points[i].x - points[j].x); // calculate slope b = (double)points[i].y - k*points[i].x; // calculate intercept } for (int m = 0; m < points.length; ++m) { // traverse all points if ((vertical && points[m].x == points[j].x) || (!vertical && Math.abs((double)points[m].y - (k*points[m].x + b)) < 0.01)) { // not necessarily 0.01; could be any reasonable delta ++count; } } max = Math.max(max, count); } } return max; }}
Solution 2: Traverse all the points; for each point, calculate the slope of the straight line this particular point and every other point could form, also count the number of points that yield in the same slope; store the slope and counter as a key-value pair in a hashmap; find the slope that has most points on it and update the global max.
Code 2:
/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y = b; } * } */public class Solution { public int maxPoints(Point[] points) { if (points == null) { return 0; } if (points.length < 3) { return points.length; } int max = 0; // global max for (int i = 0; i < points.length; ++i) { Map<Double, Integer> map = new HashMap<>(); int vertical = 1; int same = 0; int count = 1; double k = 0; // slope for (int j = 0; j < points.length; ++j) { if (i == j) { continue; } if (points[i].x == points[j].x && points[i].y == points[j].y) { ++same; // don't forget about this case } else if (points[i].x == points[j].x) { ++vertical; } else { k = (double)(points[i].y - points[j].y)/(points[i].x - points[j].x); if (map.containsKey(k)) { map.put(k, map.get(k) + 1); } else { map.put(k, 2); } } } for (Integer value : map.values()) { count = Math.max(count, value); } count += same; count = Math.max(count, vertical); max = Math.max(count, max); } return max; }}
- Max Points on a Line | leetcode
- Max Points on a Line LEETCODE
- Leetcode: Max Points on a Line
- LeetCode - Max Points on a Line
- LeetCode 之 Max Points on a Line
- LeetCode | Max Points on a Line
- [LeetCode] Max Points on a Line
- Leetcode: Max Points on a Line
- LeetCode OJ:Max Points on a Line
- Leetcode Max Points on a Line
- Leetcode: Max Points on a Line .
- [Leetcode] Max Points on a Line (Java)
- leetcode:【Max Points on a Line】
- Max Points on a Line - LeetCode
- LeetCode:Max Points on a Line
- [LeetCode] Max Points on a Line
- leetcode - Max Points on a Line
- LeetCode(149)Max Points on a Line
- Android利用泛型和反射来实现对数据库的操作--SqlHelper
- 借助 Adobe Scout 理解 Flash Player
- Android中canvas.drawText()无法使用\r\n换行的解决方案
- 2014-11-13
- 下拉列表的样式
- Leetcode: Max Points on a Line
- WorksheetFunction 成员 (Excel)
- Yii1.1 实现简单restful 框架
- caffe源码分析--Blob类代码研究
- 类模板声明与定义为什么不能分离
- git 命令纪要
- AWS Ubuntu下安装LAMP
- 稀疏矩阵A和B,其行数和列数对应相等,编写一个程序,计算A和B之和,假设稀疏矩阵采用三元组表示
- LeetCode: Spiral Matrix