Skip to content

Commit b9bc824

Browse files
committed
✨feat: Add 149
1 parent 0c7ba38 commit b9bc824

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

Index/哈希表.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| [36. 有效的数独](https://leetcode-cn.com/problems/valid-sudoku/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-sudoku/solution/ha-xi-biao-jie-fa-shu-zu-jie-fa-by-ac_oi-feac/) | 中等 | 🤩🤩🤩🤩 |
88
| [137. 只出现一次的数字 II](https://leetcode-cn.com/problems/single-number-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/single-number-ii/solution/gong-shui-san-xie-yi-ti-san-jie-ha-xi-bi-fku8/) | 中等 | 🤩🤩🤩 |
99
| [146. LRU 缓存机制](https://leetcode-cn.com/problems/lru-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lru-cache/solution/gong-shui-san-xie-she-ji-shu-ju-jie-gou-68hv2/) | 中等 | 🤩🤩🤩🤩🤩 |
10+
| [149. 直线上最多的点数](https://leetcode-cn.com/problems/max-points-on-a-line/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-points-on-a-line/solution/gong-shui-san-xie-liang-chong-mei-ju-zhi-u44s/) | 困难 | 🤩🤩🤩 |
1011
| [460. LFU 缓存](https://leetcode-cn.com/problems/lfu-cache/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/lfu-cache/solution/gong-shui-san-xie-yun-yong-tong-pai-xu-s-53m3/) | 困难 | 🤩🤩🤩🤩🤩 |
1112
| [554. 砖墙](https://leetcode-cn.com/problems/brick-wall/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/brick-wall/solution/gong-shui-san-xie-zheng-nan-ze-fan-shi-y-gsri/) | 中等 | 🤩🤩🤩 |
1213
| [692. 前K个高频单词](https://leetcode-cn.com/problems/top-k-frequent-words/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/top-k-frequent-words/solution/gong-shui-san-xie-xiang-jie-shi-yong-ha-8dxt2/) | 中等 | 🤩🤩🤩 |

Index/数学.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
| [31. 下一个排列](https://leetcode-cn.com/problems/next-permutation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/next-permutation/solution/miao-dong-xi-lie-100-cong-xia-yi-ge-pai-gog8j/) | 中等 | 🤩🤩🤩 |
99
| [42. 接雨水](https://leetcode-cn.com/problems/trapping-rain-water/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/trapping-rain-water/solution/po-su-jie-fa-on2-cha-zhao-you-hua-on-dan-iu44/) | 困难 | 🤩🤩 |
1010
| [43. 字符串相乘](https://leetcode-cn.com/problems/multiply-strings/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/multiply-strings/solution/zhi-yao-ni-hui-shou-suan-cheng-fa-zhe-ti-ainl/) | 中等 | 🤩🤩🤩🤩 |
11+
| [149. 直线上最多的点数](https://leetcode-cn.com/problems/max-points-on-a-line/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-points-on-a-line/solution/gong-shui-san-xie-liang-chong-mei-ju-zhi-u44s/) | 困难 | 🤩🤩🤩 |
1112
| [231. 2 的幂](https://leetcode-cn.com/problems/power-of-two/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/power-of-two/solution/gong-shui-san-xie-2-de-mi-by-ac_oier-qm6e/) | 简单 | 🤩🤩🤩 |
1213
| [263. 丑数](https://leetcode-cn.com/problems/ugly-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ugly-number/solution/gong-shui-san-xie-jian-dan-de-fen-qing-k-dlvg/) | 简单 | 🤩🤩 |
1314
| [342. 4的幂](https://leetcode-cn.com/problems/power-of-four/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/power-of-four/solution/gong-shui-san-xie-zhuan-hua-wei-2-de-mi-y21lq/) | 简单 | 🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[149. 直线上最多的点数](https://leetcode-cn.com/problems/max-points-on-a-line/solution/gong-shui-san-xie-liang-chong-mei-ju-zhi-u44s/)** ,难度为 **困难**
4+
5+
Tag : 「数学」、「枚举」、「哈希表」
6+
7+
给你一个数组 points ,其中 points[i] = [xi, yi] 表示 X-Y 平面上的一个点。求最多有多少个点在同一条直线上。
8+
9+
10+
示例 1:
11+
![](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg)
12+
```
13+
输入:points = [[1,1],[2,2],[3,3]]
14+
输出:3
15+
```
16+
示例 2:
17+
![](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg)
18+
```
19+
输入:points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
20+
输出:4
21+
```
22+
23+
提示:
24+
* 1 <= points.length <= 300
25+
* points[i].length == 2
26+
* -$10^4$ <= xi, yi <= $10^4$
27+
* points 中的所有点 互不相同
28+
29+
---
30+
31+
### 朴素解法(枚举直线 + 枚举统计)
32+
33+
我们知道,两个点可以确定一条线。
34+
35+
因此一个朴素的做法是先枚举两条点(确定一条线),然后检查其余点是否落在该线中。
36+
37+
为了避免除法精度问题,当我们枚举两个点 $i$ 和 $j$ 时,不直接计算其对应直线的 `斜率``截距`,而是通过判断 $i$ 和 $j$ 与第三个点 $k$ 形成的两条直线斜率是否相等(斜率相等的两条直线要么平行,要么重合,平行需要 $4$ 个点来唯一确定,我们只有 $3$ 个点,所以可以直接判定两直线重合)。
38+
39+
代码:
40+
```Java []
41+
class Solution {
42+
public int maxPoints(int[][] ps) {
43+
int n = ps.length;
44+
int ans = 1;
45+
for (int i = 0; i < n; i++) {
46+
int[] x = ps[i];
47+
for (int j = i + 1; j < n; j++) {
48+
int[] y = ps[j];
49+
int cnt = 2;
50+
for (int k = j + 1; k < n; k++) {
51+
int[] p = ps[k];
52+
int s1 = (y[1] - x[1]) * (p[0] - y[0]);
53+
int s2 = (p[1] - y[1]) * (y[0] - x[0]);
54+
if (s1 == s2) cnt++;
55+
}
56+
ans = Math.max(ans, cnt);
57+
}
58+
}
59+
return ans;
60+
}
61+
}
62+
```
63+
* 时间复杂度:$O(n^3)$
64+
* 空间复杂度:$O(1)$
65+
66+
---
67+
68+
### 优化(枚举直线 + 哈希表统计)
69+
70+
根据「朴素解法」的思路,枚举所有直线的过程不可避免,但统计点数的过程可以优化。
71+
72+
具体的,我们可以先枚举所有可能出现的 `直线斜率`(根据两点确定一条直线,即枚举所有的「点对」),使用「哈希表」统计所有 `斜率` 对应的点的数量,在所有值中取个 $max$ 即是答案。
73+
74+
一些细节:在使用「哈希表」进行保存时,为了避免精度问题,我们直接使用字符串进行保存,同时需要将 `斜率` 约干净。
75+
76+
代码:
77+
```Java []
78+
class Solution {
79+
public int maxPoints(int[][] ps) {
80+
int n = ps.length;
81+
int ans = 1;
82+
for (int i = 0; i < n; i++) {
83+
Map<String, Integer> map = new HashMap<>();
84+
// 由当前点 i 发出的直线所经过的最多点数量
85+
int max = 0;
86+
for (int j = i + 1; j < n; j++) {
87+
int x1 = ps[i][0], y1 = ps[i][1], x2 = ps[j][0], y2 = ps[j][1];
88+
int a = x1 - x2, b = y1 - y2;
89+
int k = gcd(a, b);
90+
String key = (a / k) + "_" + (b / k);
91+
map.put(key, map.getOrDefault(key, 0) + 1);
92+
max = Math.max(max, map.get(key));
93+
}
94+
ans = Math.max(ans, max + 1);
95+
}
96+
return ans;
97+
}
98+
int gcd(int a, int b) {
99+
return b == 0 ? a : gcd(b, a % b);
100+
}
101+
}
102+
```
103+
* 时间复杂度:枚举所有直线的复杂度为 $O(n^2)$;令坐标值的最大差值为 $m$,`gcd` 复杂度为 $O(\log{m})$。整体复杂度为 $O(n^2 * \log{m})$
104+
* 空间复杂度:$O(n)$
105+
106+
---
107+
108+
### 最后
109+
110+
这是我们「刷穿 LeetCode」系列文章的第 `No.149` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。
111+
112+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
113+
114+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
115+
116+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
117+

0 commit comments

Comments
 (0)