149-Max Points on a Line
Description
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],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation:
^
|
| o
| o o
| o
| o o
+------------------->
0 1 2 3 4 5 6
問題描述
在一個2維的平面上給定n個點, 返回在同一條直線上的最多的點的個數
問題分析
每輪以一個點為端點, 算出對應的在同一條直線上的最多的點的個數, 然後與當前的最大值比較即可
解法
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
class Solution {
public int maxPoints(Point[] points) {
if(points.length <= 2) return points.length;
//儲存每輪的結果
Map<Integer, Map<Integer, Integer>> map = new HashMap();
int res = 0, len = points.length;
for(int i = 0;i < len;i++){
//每輪進行處理前先清空上一輪的結果
map.clear();
int x = points[i].x;
int y = points[i].y;
//重複的點的個數
int overlap = 1;
int max = 0;
//注意這裡是i + 1
for(int j = i + 1;j < len;j++){
int kx = points[j].x;
int ky = points[j].y;
int dy = ky - y;
int dx = kx - x;
if(dy == 0 && dx == 0){
overlap++;
continue;
}
//通過最大公約數對斜率化簡
int g = gcd(dy, dx);
dy = dy / g;
dx = dx / g;
//如果有過相同斜率的點, 那麼在一條直線上
if(map.containsKey(dy)){
map.get(dy).put(dx, map.get(dy).getOrDefault(dx, 0) + 1);
}else{
Map<Integer, Integer> temp = new HashMap();
temp.put(dx, 1);
map.put(dy, temp);
}
max = Math.max(max, map.get(dy).get(dx));
}
//注意這裡需要加上overlap
res = Math.max(res, overlap + max);
}
return res;
}
//求最大公約數
public int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a % b);
}
}
相關文章
- leetcode-149-Max Points on a LineLeetCode
- 【窮舉】Max Points on a Line平面上共線的點
- Grid Points
- Machine Learning - Basic pointsMac
- OpenGL Shader Key Points (3)
- POJ - 3090 Visible Lattice Points
- Inspection Points: Key settings and Usage
- HDU 4347 The Closest M Points
- eslint-disable-next-line to ignore the next lineEsLint
- Objects as Points 論文總結Object
- Command Line Tools
- [LeetCode] 3096. Minimum Levels to Gain More PointsLeetCodeAI
- guarantee restore points-Flashback after RMAN restoreREST
- PAT A1048 二分/two points
- 論文解讀(LINE)《LINE: Large-scale Information Network Embedding》ORM
- 【node】command-line
- change ^M to new line
- Python Line Messaging ApiPythonAPI
- SVG <line> 直線SVG
- Network Embedding_LINE
- HDU 3400 Line belt
- Solaris awk: syntax error near line 1 awk: bailing out near line 1ErrorAI
- 什麼是 Angular library 的 secondary entry points?Angular
- halcon change_radial_distortion_points運算元
- Command line is too long. Shorten command line for JooLunMallApiApplication or also for Spring Boot default configuration?APIAPPSpring Boot
- 【Linux shell】while read lineLinuxWhile
- idea遇見Command line is too long. Shorten command line for Main or also for Application default configuration?IdeaAIAPP
- WPF customize line with sharp arrow and direction
- 教你做Line鬧鐘
- Azure Command Line (一)入門
- CSS line-height 行高CSS
- ErrorException In FilesystemAdapter.php line 207ErrorExceptionAPTPHP
- 973. K Closest Points to Origin(Leetcode每日一題-2020.11.09)LeetCode每日一題
- Error running ‘Application’Command line is too longErrorAPP
- [20210930]IFS= read -r line.txt
- 【貪心】POJ 3617:Best Cow Line
- Binary XML file line #2: Error inflatingXMLError
- SAP PM入門系列24 - IK07 Display Measuring Points