Skip to content

Commit c0ac1e6

Browse files
committed
feat: add solutions to lcci problem: No.16.14
No.16.14.Best Line
1 parent 28b467c commit c0ac1e6

File tree

7 files changed

+570
-0
lines changed

7 files changed

+570
-0
lines changed

lcci/16.14.Best Line/README.md

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,261 @@
2121
## 解法
2222

2323
<!-- 这里可写通用的实现逻辑 -->
24+
25+
**方法一:暴力枚举**
26+
27+
我们可以枚举任意两个点 $(x_1, y_1), (x_2, y_2)$,把这两个点连成一条直线,那么此时这条直线上的点的个数就是 2,接下来我们再枚举其他点 $(x_3, y_3)$,判断它们是否在同一条直线上,如果在,那么直线上的点的个数就加 1,如果不在,那么直线上的点的个数不变。找出所有直线上的点的个数的最大值,其对应的最小的两个点的编号即为答案。
28+
29+
时间复杂度 $O(n^3)$,空间复杂度 $O(1)$。其中 $n$ 是数组 `points` 的长度。
30+
31+
**方法二:枚举 + 哈希表**
32+
33+
我们可以枚举一个点 $(x_1, y_1)$,把其他所有点 $(x_2, y_2)$ 与 $(x_1, y_1)$ 连成的直线的斜率存入哈希表中,斜率相同的点在同一条直线上,哈希表的键为斜率,值为直线上的点的个数。找出哈希表中的最大值,即为答案。为了避免精度问题,我们可以将斜率 $\frac{y_2 - y_1}{x_2 - x_1}$ 进行约分,约分的方法是求最大公约数,然后分子分母同时除以最大公约数,将求得的分子分母作为哈希表的键。
34+
35+
时间复杂度 $O(n^2 \times \log m)$,空间复杂度 $O(n)$。其中 $n$ 和 $m$ 分别是数组 `points` 的长度和数组 `points` 所有横纵坐标差的最大值。
36+
37+
相似题目:
38+
39+
- [149. 直线上最多的点数](/solution/0100-0199/0149.Max%20Points%20on%20a%20Line/README.md)
40+
2441
<!-- tabs:start -->
2542

2643
### **Python3**
2744

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

3047
```python
48+
class Solution:
49+
def bestLine(self, points: List[List[int]]) -> List[int]:
50+
n = len(points)
51+
mx = 0
52+
for i in range(n):
53+
x1, y1 = points[i]
54+
for j in range(i + 1, n):
55+
x2, y2 = points[j]
56+
cnt = 2
57+
for k in range(j + 1, n):
58+
x3, y3 = points[k]
59+
a = (y2 - y1) * (x3 - x1)
60+
b = (y3 - y1) * (x2 - x1)
61+
cnt += a == b
62+
if mx < cnt:
63+
mx = cnt
64+
x, y = i, j
65+
return [x, y]
66+
```
3167

68+
```python
69+
class Solution:
70+
def bestLine(self, points: List[List[int]]) -> List[int]:
71+
def gcd(a, b):
72+
return a if b == 0 else gcd(b, a % b)
73+
74+
n = len(points)
75+
mx = 0
76+
for i in range(n):
77+
x1, y1 = points[i]
78+
cnt = defaultdict(list)
79+
for j in range(i + 1, n):
80+
x2, y2 = points[j]
81+
dx, dy = x2 - x1, y2 - y1
82+
g = gcd(dx, dy)
83+
k = (dx // g, dy // g)
84+
cnt[k].append((i, j))
85+
if mx < len(cnt[k]) or (mx == len(cnt[k]) and (x, y) > cnt[k][0]):
86+
mx = len(cnt[k])
87+
x, y = cnt[k][0]
88+
return [x, y]
3289
```
3390

3491
### **Java**
3592

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

3895
```java
96+
class Solution {
97+
public int[] bestLine(int[][] points) {
98+
int n = points.length;
99+
int mx = 0;
100+
int[] ans = new int[2];
101+
for (int i = 0; i < n; ++i) {
102+
int x1 = points[i][0], y1 = points[i][1];
103+
for (int j = i + 1; j < n; ++j) {
104+
int x2 = points[j][0], y2 = points[j][1];
105+
int cnt = 2;
106+
for (int k = j + 1; k < n; ++k) {
107+
int x3 = points[k][0], y3 = points[k][1];
108+
int a = (y2 - y1) * (x3 - x1);
109+
int b = (y3 - y1) * (x2 - x1);
110+
if (a == b) {
111+
++cnt;
112+
}
113+
}
114+
if (mx < cnt) {
115+
mx = cnt;
116+
ans[0] = i;
117+
ans[1] = j;
118+
}
119+
}
120+
}
121+
return ans;
122+
}
123+
}
124+
```
125+
126+
```java
127+
class Solution {
128+
public int[] bestLine(int[][] points) {
129+
int n = points.length;
130+
int mx = 0;
131+
int[] ans = new int[2];
132+
for (int i = 0; i < n; ++i) {
133+
int x1 = points[i][0], y1 = points[i][1];
134+
Map<String, List<int[]>> cnt = new HashMap<>();
135+
for (int j = i + 1; j < n; ++j) {
136+
int x2 = points[j][0], y2 = points[j][1];
137+
int dx = x2 - x1, dy = y2 - y1;
138+
int g = gcd(dx, dy);
139+
String key = (dx / g) + "." + (dy / g);
140+
cnt.computeIfAbsent(key, k -> new ArrayList<>()).add(new int[] {i, j});
141+
if (mx < cnt.get(key).size() || (mx == cnt.get(key).size() && (ans[0] > cnt.get(key).get(0)[0] || (ans[0] == cnt.get(key).get(0)[0] && ans[1] > cnt.get(key).get(0)[1])))) {
142+
mx = cnt.get(key).size();
143+
ans = cnt.get(key).get(0);
144+
}
145+
}
146+
}
147+
return ans;
148+
}
149+
150+
private int gcd(int a, int b) {
151+
return b == 0 ? a : gcd(b, a % b);
152+
}
153+
}
154+
```
155+
156+
### **C++**
157+
158+
```cpp
159+
class Solution {
160+
public:
161+
vector<int> bestLine(vector<vector<int>>& points) {
162+
int n = points.size();
163+
int mx = 0;
164+
vector<int> ans(2);
165+
for (int i = 0; i < n; ++i) {
166+
int x1 = points[i][0], y1 = points[i][1];
167+
for (int j = i + 1; j < n; ++j) {
168+
int x2 = points[j][0], y2 = points[j][1];
169+
int cnt = 2;
170+
for (int k = j + 1; k < n; ++k) {
171+
int x3 = points[k][0], y3 = points[k][1];
172+
long a = (long) (y2 - y1) * (x3 - x1);
173+
long b = (long) (y3 - y1) * (x2 - x1);
174+
cnt += a == b;
175+
}
176+
if (mx < cnt) {
177+
mx = cnt;
178+
ans[0] = i;
179+
ans[1] = j;
180+
}
181+
}
182+
}
183+
return ans;
184+
}
185+
};
186+
```
187+
188+
```cpp
189+
class Solution {
190+
public:
191+
vector<int> bestLine(vector<vector<int>>& points) {
192+
int n = points.size();
193+
int mx = 0;
194+
pair<int, int> ans = {0, 0};
195+
for (int i = 0; i < n; ++i) {
196+
int x1 = points[i][0], y1 = points[i][1];
197+
unordered_map<string, vector<pair<int, int>>> cnt;
198+
for (int j = i + 1; j < n; ++j) {
199+
int x2 = points[j][0], y2 = points[j][1];
200+
int dx = x2 - x1, dy = y2 - y1;
201+
int g = gcd(dx, dy);
202+
string k = to_string(dx / g) + "." + to_string(dy / g);
203+
cnt[k].push_back({i, j});
204+
if (mx < cnt[k].size() || (mx == cnt[k].size() && ans > cnt[k][0])) {
205+
mx = cnt[k].size();
206+
ans = cnt[k][0];
207+
}
208+
}
209+
}
210+
return vector<int>{ans.first, ans.second};
211+
}
212+
213+
int gcd(int a, int b) {
214+
return b == 0 ? a : gcd(b, a % b);
215+
}
216+
};
217+
```
218+
219+
### **Go**
220+
221+
```go
222+
func bestLine(points [][]int) []int {
223+
n := len(points)
224+
ans := make([]int, 2)
225+
mx := 0
226+
for i := 0; i < n; i++ {
227+
x1, y1 := points[i][0], points[i][1]
228+
for j := i + 1; j < n; j++ {
229+
x2, y2 := points[j][0], points[j][1]
230+
cnt := 2
231+
for k := j + 1; k < n; k++ {
232+
x3, y3 := points[k][0], points[k][1]
233+
a := (y2 - y1) * (x3 - x1)
234+
b := (y3 - y1) * (x2 - x1)
235+
if a == b {
236+
cnt++
237+
}
238+
}
239+
if mx < cnt {
240+
mx = cnt
241+
ans[0], ans[1] = i, j
242+
}
243+
}
244+
}
245+
return ans
246+
}
247+
```
248+
249+
```go
250+
func bestLine(points [][]int) []int {
251+
n := len(points)
252+
ans := make([]int, 2)
253+
type pair struct{ i, j int }
254+
mx := 0
255+
for i := 0; i < n; i++ {
256+
x1, y1 := points[i][0], points[i][1]
257+
cnt := map[pair][]pair{}
258+
for j := i + 1; j < n; j++ {
259+
x2, y2 := points[j][0], points[j][1]
260+
dx, dy := x2-x1, y2-y1
261+
g := gcd(dx, dy)
262+
k := pair{dx / g, dy / g}
263+
cnt[k] = append(cnt[k], pair{i, j})
264+
if mx < len(cnt[k]) || (mx == len(cnt[k]) && (ans[0] > cnt[k][0].i || (ans[0] == cnt[k][0].i && ans[1] > cnt[k][0].j))) {
265+
mx = len(cnt[k])
266+
ans[0], ans[1] = cnt[k][0].i, cnt[k][0].j
267+
}
268+
}
269+
}
270+
return ans
271+
}
39272

273+
func gcd(a, b int) int {
274+
if b == 0 {
275+
return a
276+
}
277+
return gcd(b, a%b)
278+
}
40279
```
41280

42281
### **...**

0 commit comments

Comments
 (0)