当前位置:网站首页>LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
2022-07-05 20:58:00 【Old street of small town】
1. subject
Give you an array points , among points[i] = [xi, yi] Express X-Y A point on the plane . Find out how many points are in the same line at most .
Example 1:
Input :points = [[1,1],[2,2],[3,3]]
Output :3
Example 2:
Input :points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output :4
Tips :
1 <= points.length <= 300
points[i].length == 2
-104 <= xi, yi <= 104
points All points in are different from each other
source : Power button (LeetCode)
link :https://leetcode.cn/problems/max-points-on-a-line
2. Ideas
(1) The law of violent exhaustion
Violent exhaustion is easier to think of , Because two different points in the plane can determine a straight line , So you can First enumerate the combination of two points ( That is to determine a straight line ), Then check whether the remaining points are on the straight line .
(2) Improvement of violent exhaustion _ Hashtable
Train of thought reference The user's question solution .
Because two points determine a straight line , So we can enumerate all the possible slope of the straight line ( That is, enumerate all point pairs ), Then use the hash table to count the number of points on the straight line corresponding to all slopes ( Instant key / value = Slope / The number of points ), Then the maximum of all values is the answer .
3. Code implementation (Java)
// Ideas 1———— The law of violent exhaustion
class Solution {
public int maxPoints(int[][] points) {
// At most cnt The points are on the same straight line , because 1 <= points.length <= 300, therefore cnt The initial value of 1
int cnt = 1;
int n = points.length;
for (int i = 0; i < n; i++) {
int[] p1 = points[i];
for (int j = i + 1; j < n; j++) {
int[] p2 = points[j];
//p1 And p2 It must be in a straight line , So at least 2 The points are on the same straight line
int tmpCnt = 2;
for (int k = j + 1; k < n; k++) {
int[] p3 = points[k];
/* (1) If (p1[1] - p2[1])/(p1[0] - p2[0]) == (p2[1] - p3[1])/(p2[0] - p3[0]) that p1、p2、p3 this 3 The points are on the same straight line (2) In order to prevent the loss of accuracy when dividing integers , So change the above equation to : (p1[0] - p2[0]) * (p2[1] - p3[1]) = (p1[1] - p2[1]) * (p2[0] - p3[0]) */
if ((p1[0] - p2[0]) * (p2[1] - p3[1]) == (p1[1] - p2[1]) * (p2[0] - p3[0])) {
tmpCnt++;
}
}
cnt = Math.max(tmpCnt, cnt);
}
}
return cnt;
}
}
// Ideas 2———— Improvement of violent exhaustion _ Hashtable
class Solution {
public int maxPoints(int[][] ps) {
int n = ps.length;
int cnt = 1;
for (int i = 0; i < n; i++) {
Map<String, Integer> hashMap = new HashMap<>();
// From the current point i The maximum number of points passed by the emitted straight line
int max = 0;
for (int j = i + 1; j < n; j++) {
int x1 = ps[i][0], y1 = ps[i][1], x2 = ps[j][0], y2 = ps[j][1];
int a = x1 - x2, b = y1 - y2;
int k = gcd(a, b);
String key = (a / k) + "_" + (b / k);
hashMap.put(key, hashMap.getOrDefault(key, 0) + 1);
max = Math.max(max, hashMap.get(key));
}
cnt = Math.max(cnt, max + 1);
}
return cnt;
}
// seek a and b Maximum common divisor of
public int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
边栏推荐
- Which is the best online collaboration product? Microsoft loop, notion, flowus
- 研學旅遊實踐教育的開展助力文旅產業發展
- Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
- Duchefa low melting point agarose PPC Chinese and English instructions
- The development of research tourism practical education helps the development of cultural tourism industry
- Abnova CRISPR spcas9 polyclonal antibody protocol
- 示波器探头对信号源阻抗的影响
- Generics of TS
- Chemical properties and application instructions of prosci Lag3 antibody
- Graph embedding learning notes
猜你喜欢

Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing

Use of thread pool

如何让化工企业的ERP库存账目更准确

基于AVFoundation实现视频录制的两种方式

Abnova total RNA Purification Kit for cultured cells Chinese and English instructions

重上吹麻滩——段芝堂创始人翟立冬游记

解析创客教育的知识迁移和分享精神

请查收.NET MAUI 的最新学习资源

中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097

Analysis of steam education mode under the integration of five Education
随机推荐
AITM 2-0003 水平燃烧试验
概率论机器学习的先验知识(上)
poj 3414 Pots (bfs+线索)
Abnova CD81 monoclonal antibody related parameters and Applications
解读协作型机器人的日常应用功能
Traps in the explode function in PHP
Material Design组件 - 使用BottomSheet展现扩展内容(二)
leetcode:1139. 最大的以 1 为边界的正方形
systemd-resolved 开启 debug 日志
Careercup its 1.8 serial shift includes problems
Comparison table of foreign lead American abbreviations
Enclosed please find. Net Maui's latest learning resources
研學旅遊實踐教育的開展助力文旅產業發展
ts 之 属性的修饰符public、private、protect
Maker education infiltrating the transformation of maker spirit and culture
产品好不好,谁说了算?Sonar提出分析的性能指标,帮助您轻松判断产品性能及表现
Who the final say whether the product is good or not? Sonar puts forward performance indicators for analysis to help you easily judge product performance and performance
示波器探头对测量带宽的影响
Prosci LAG-3 recombinant protein specification
vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解