Skip to content

Commit 3e7dfab

Browse files
committedMay 3, 2021
feat: add solutions to leetcode problem: No.0149. Max Points on a Line
1 parent 5b94bbc commit 3e7dfab

File tree

9 files changed

+185
-32
lines changed

9 files changed

+185
-32
lines changed
 

‎solution/0100-0199/0149.Max Points on a Line/README.md

+66-3
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,90 @@
3636
+------------------->
3737
0 &nbsp;1 &nbsp;2 &nbsp;3 &nbsp;4 &nbsp;5 &nbsp;6</pre>
3838

39-
4039
## 解法
4140

4241
<!-- 这里可写通用的实现逻辑 -->
4342

43+
在平面上确定一个点 `points[i]`,其他点与 `point[i]` 可以求得一个斜率,斜率相同的点意味着它们与 `points[i]` 在同一条直线上。
44+
45+
所以可以用哈希表作为计数器,其中斜率作为 key,然后累计当前点相同的斜率出现的次数。斜率可能是小数,我们可以用分数形式表示,先求分子分母的最大公约数,然后约分,最后将“分子.分母” 作为 key 即可。
46+
47+
需要注意,如果平面上有和当前点重叠的点,如果进行约分,会出现除 0 的情况,那么我们单独用一个变量 duplicate 统计重复点的个数,重复点一定是过当前点的直线的。
48+
4449
<!-- tabs:start -->
4550

4651
### **Python3**
4752

4853
<!-- 这里可写当前语言的特殊实现逻辑 -->
4954

5055
```python
51-
56+
class Solution:
57+
def maxPoints(self, points: List[List[int]]) -> int:
58+
def gcd(a, b) -> int:
59+
return a if b == 0 else gcd(b, a % b)
60+
61+
n = len(points)
62+
if n < 3:
63+
return n
64+
res = 0
65+
for i in range(n - 1):
66+
counter = collections.Counter()
67+
t_max = duplicate = 0
68+
for j in range(i + 1, n):
69+
delta_x = points[i][0] - points[j][0]
70+
delta_y = points[i][1] - points[j][1]
71+
if delta_x == 0 and delta_y == 0:
72+
duplicate += 1
73+
continue
74+
g = gcd(delta_x, delta_y)
75+
d_x = delta_x // g
76+
d_y = delta_y // g
77+
key = f'{d_x}.{d_y}'
78+
counter[key] += 1
79+
t_max = max(t_max, counter[key])
80+
res = max(res, t_max + duplicate + 1)
81+
return res
5282
```
5383

5484
### **Java**
5585

5686
<!-- 这里可写当前语言的特殊实现逻辑 -->
5787

5888
```java
59-
89+
class Solution {
90+
public int maxPoints(int[][] points) {
91+
int n = points.length;
92+
if (n < 3) {
93+
return n;
94+
}
95+
int res = 0;
96+
for (int i = 0; i < n - 1; ++i) {
97+
Map<String, Integer> kCounter = new HashMap<>();
98+
int max = 0;
99+
int duplicate = 0;
100+
for (int j = i + 1; j < n; ++j) {
101+
int deltaX = points[i][0] - points[j][0];
102+
int deltaY = points[i][1] - points[j][1];
103+
if (deltaX == 0 && deltaY == 0) {
104+
++duplicate;
105+
continue;
106+
}
107+
int gcd = gcd(deltaX, deltaY);
108+
int dX = deltaX / gcd;
109+
int dY = deltaY / gcd;
110+
String key = dX + "." + dY;
111+
kCounter.put(key, kCounter.getOrDefault(key, 0) + 1);
112+
max = Math.max(max, kCounter.get(key));
113+
}
114+
res = Math.max(res, max + duplicate + 1);
115+
}
116+
return res;
117+
}
118+
119+
private int gcd(int a, int b) {
120+
return b == 0 ? a : gcd(b, a % b);
121+
}
122+
}
60123
```
61124

62125
### **...**

‎solution/0100-0199/0149.Max Points on a Line/README_EN.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,78 @@
3131
<li>All the <code>points</code> are <strong>unique</strong>.</li>
3232
</ul>
3333

34-
3534
## Solutions
3635

3736
<!-- tabs:start -->
3837

3938
### **Python3**
4039

4140
```python
42-
41+
class Solution:
42+
def maxPoints(self, points: List[List[int]]) -> int:
43+
def gcd(a, b) -> int:
44+
return a if b == 0 else gcd(b, a % b)
45+
46+
n = len(points)
47+
if n < 3:
48+
return n
49+
res = 0
50+
for i in range(n - 1):
51+
counter = collections.Counter()
52+
t_max = duplicate = 0
53+
for j in range(i + 1, n):
54+
delta_x = points[i][0] - points[j][0]
55+
delta_y = points[i][1] - points[j][1]
56+
if delta_x == 0 and delta_y == 0:
57+
duplicate += 1
58+
continue
59+
g = gcd(delta_x, delta_y)
60+
d_x = delta_x // g
61+
d_y = delta_y // g
62+
key = f'{d_x}.{d_y}'
63+
counter[key] += 1
64+
t_max = max(t_max, counter[key])
65+
res = max(res, t_max + duplicate + 1)
66+
return res
4367
```
4468

4569
### **Java**
4670

4771
```java
48-
72+
class Solution {
73+
public int maxPoints(int[][] points) {
74+
int n = points.length;
75+
if (n < 3) {
76+
return n;
77+
}
78+
int res = 0;
79+
for (int i = 0; i < n - 1; ++i) {
80+
Map<String, Integer> kCounter = new HashMap<>();
81+
int max = 0;
82+
int duplicate = 0;
83+
for (int j = i + 1; j < n; ++j) {
84+
int deltaX = points[i][0] - points[j][0];
85+
int deltaY = points[i][1] - points[j][1];
86+
if (deltaX == 0 && deltaY == 0) {
87+
++duplicate;
88+
continue;
89+
}
90+
int gcd = gcd(deltaX, deltaY);
91+
int dX = deltaX / gcd;
92+
int dY = deltaY / gcd;
93+
String key = dX + "." + dY;
94+
kCounter.put(key, kCounter.getOrDefault(key, 0) + 1);
95+
max = Math.max(max, kCounter.get(key));
96+
}
97+
res = Math.max(res, max + duplicate + 1);
98+
}
99+
return res;
100+
}
101+
102+
private int gcd(int a, int b) {
103+
return b == 0 ? a : gcd(b, a % b);
104+
}
105+
}
49106
```
50107

51108
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
class Solution {
2-
public int maxPoints(Point[] points) {
3-
if( points.length <= 2 ) return points.length;
4-
int max = 2 ;
5-
for( int i = 0 ; i < points.length ; i++ ){
6-
int samePosition = 0;
7-
int sameSlope = 1;
8-
for( int j = i + 1 ; j < points.length ; j++ ){
9-
long x1 = points[j].x - points[i].x;
10-
long y1 = points[j].y - points[i].y;
11-
if( x1 == 0 && y1 == 0 ){
12-
samePosition++;
13-
} else {
14-
sameSlope++;
15-
for(int k = j + 1 ; k < points.length ; k++ ){
16-
long x2 = points[k].x - points[i].x;
17-
long y2 = points[k].y - points[i].y;
18-
if ( x1 * y2 == x2 * y1 ) sameSlope++;
19-
}
2+
public int maxPoints(int[][] points) {
3+
int n = points.length;
4+
if (n < 3) {
5+
return n;
6+
}
7+
int res = 0;
8+
for (int i = 0; i < n - 1; ++i) {
9+
Map<String, Integer> kCounter = new HashMap<>();
10+
int max = 0;
11+
int duplicate = 0;
12+
for (int j = i + 1; j < n; ++j) {
13+
int deltaX = points[i][0] - points[j][0];
14+
int deltaY = points[i][1] - points[j][1];
15+
if (deltaX == 0 && deltaY == 0) {
16+
++duplicate;
17+
continue;
2018
}
21-
if(max < (samePosition + sameSlope)) max = samePosition + sameSlope;
22-
sameSlope = 1;
19+
int gcd = gcd(deltaX, deltaY);
20+
int dX = deltaX / gcd;
21+
int dY = deltaY / gcd;
22+
String key = dX + "." + dY;
23+
kCounter.put(key, kCounter.getOrDefault(key, 0) + 1);
24+
max = Math.max(max, kCounter.get(key));
2325
}
26+
res = Math.max(res, max + duplicate + 1);
2427
}
25-
return max;
28+
return res;
29+
}
30+
31+
private int gcd(int a, int b) {
32+
return b == 0 ? a : gcd(b, a % b);
2633
}
2734
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def maxPoints(self, points: List[List[int]]) -> int:
3+
def gcd(a, b) -> int:
4+
return a if b == 0 else gcd(b, a % b)
5+
6+
n = len(points)
7+
if n < 3:
8+
return n
9+
res = 0
10+
for i in range(n - 1):
11+
counter = collections.Counter()
12+
t_max = duplicate = 0
13+
for j in range(i + 1, n):
14+
delta_x = points[i][0] - points[j][0]
15+
delta_y = points[i][1] - points[j][1]
16+
if delta_x == 0 and delta_y == 0:
17+
duplicate += 1
18+
continue
19+
g = gcd(delta_x, delta_y)
20+
d_x = delta_x // g
21+
d_y = delta_y // g
22+
key = f'{d_x}.{d_y}'
23+
counter[key] += 1
24+
t_max = max(t_max, counter[key])
25+
res = max(res, t_max + duplicate + 1)
26+
return res

‎solution/0400-0499/0447.Number of Boomerangs/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
class Solution:
6464
def numberOfBoomerangs(self, points: List[List[int]]) -> int:
6565
n = len(points)
66-
if len(points) < 3:
66+
if n < 3:
6767
return 0
6868
number = 0
6969
for i in range(n):

‎solution/0400-0499/0447.Number of Boomerangs/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
class Solution:
5454
def numberOfBoomerangs(self, points: List[List[int]]) -> int:
5555
n = len(points)
56-
if len(points) < 3:
56+
if n < 3:
5757
return 0
5858
number = 0
5959
for i in range(n):

‎solution/0400-0499/0447.Number of Boomerangs/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def numberOfBoomerangs(self, points: List[List[int]]) -> int:
33
n = len(points)
4-
if len(points) < 3:
4+
if n < 3:
55
return 0
66
number = 0
77
for i in range(n):

‎solution/summary.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@
439439
- [0429.N-ary Tree Level Order Traversal](/solution/0400-0499/0429.N-ary%20Tree%20Level%20Order%20Traversal/README.md)
440440
- [0430.Flatten a Multilevel Doubly Linked List](/solution/0400-0499/0430.Flatten%20a%20Multilevel%20Doubly%20Linked%20List/README.md)
441441
- [0431.Encode N-ary Tree to Binary Tree](/solution/0400-0499/0431.Encode%20N-ary%20Tree%20to%20Binary%20Tree/README.md)
442-
- [0432.All O`one Data Structure](/solution/0400-0499/0432.All%20O%60one%20Data%20Structure/README.md)
442+
- [0432.All O one Data Structure](/solution/0400-0499/0432.All%20O%60one%20Data%20Structure/README.md)
443443
- [0433.Minimum Genetic Mutation](/solution/0400-0499/0433.Minimum%20Genetic%20Mutation/README.md)
444444
- [0434.Number of Segments in a String](/solution/0400-0499/0434.Number%20of%20Segments%20in%20a%20String/README.md)
445445
- [0435.Non-overlapping Intervals](/solution/0400-0499/0435.Non-overlapping%20Intervals/README.md)

‎solution/summary_en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@
439439
- [0429.N-ary Tree Level Order Traversal](/solution/0400-0499/0429.N-ary%20Tree%20Level%20Order%20Traversal/README_EN.md)
440440
- [0430.Flatten a Multilevel Doubly Linked List](/solution/0400-0499/0430.Flatten%20a%20Multilevel%20Doubly%20Linked%20List/README_EN.md)
441441
- [0431.Encode N-ary Tree to Binary Tree](/solution/0400-0499/0431.Encode%20N-ary%20Tree%20to%20Binary%20Tree/README_EN.md)
442-
- [0432.All O`one Data Structure](/solution/0400-0499/0432.All%20O%60one%20Data%20Structure/README_EN.md)
442+
- [0432.All O one Data Structure](/solution/0400-0499/0432.All%20O%60one%20Data%20Structure/README_EN.md)
443443
- [0433.Minimum Genetic Mutation](/solution/0400-0499/0433.Minimum%20Genetic%20Mutation/README_EN.md)
444444
- [0434.Number of Segments in a String](/solution/0400-0499/0434.Number%20of%20Segments%20in%20a%20String/README_EN.md)
445445
- [0435.Non-overlapping Intervals](/solution/0400-0499/0435.Non-overlapping%20Intervals/README_EN.md)

0 commit comments

Comments
 (0)