Skip to content

Commit d402811

Browse files
committedSep 8, 2021
feat: add solutions to lcs problems 02,03
1 parent a7a22ee commit d402811

File tree

7 files changed

+274
-7
lines changed

7 files changed

+274
-7
lines changed
 

‎index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
name: 'leetcode',
2929
logo: '/images/doocs-leetcode.png',
3030
search: [
31-
'/', '/solution/', '/lcof/', '/lcof2/', '/lcci/', '/lcs', 'lcp', '/basic/'
31+
'/', '/solution/', '/lcof/', '/lcof2/', '/lcci/', '/lcs/', '/lcp/', '/basic/'
3232
],
3333
loadSidebar: 'summary.md',
3434
auto2top: true,

‎lcs/LCS 02. 完成一半题目/README.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,55 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39+
统计各个问题类型出现的次数,按照次数降序排列。
40+
41+
然后依次选择问题类型,直至满足条件。
42+
3943
<!-- tabs:start -->
4044

4145
### **Python3**
4246

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

4549
```python
46-
50+
class Solution:
51+
def halfQuestions(self, questions: List[int]) -> int:
52+
counter = collections.Counter(questions)
53+
counter = OrderedDict(counter.most_common())
54+
n = len(questions) >> 1
55+
res = 0
56+
for _, v in counter.items():
57+
res += 1
58+
if v >= n:
59+
return res
60+
n -= v
61+
return res
4762
```
4863

4964
### **Java**
5065

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

5368
```java
54-
69+
class Solution {
70+
public int halfQuestions(int[] questions) {
71+
int[] counter = new int[1010];
72+
for (int e : questions) {
73+
++counter[e];
74+
}
75+
int n = questions.length >> 1;
76+
Arrays.sort(counter);
77+
int res = 0;
78+
for (int i = counter.length - 1; i >= 0; --i) {
79+
++res;
80+
if (counter[i] >= n) {
81+
return res;
82+
}
83+
n -= counter[i];
84+
}
85+
return res;
86+
}
87+
}
5588
```
5689

5790
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int halfQuestions(int[] questions) {
3+
int[] counter = new int[1010];
4+
for (int e : questions) {
5+
++counter[e];
6+
}
7+
int n = questions.length >> 1;
8+
Arrays.sort(counter);
9+
int res = 0;
10+
for (int i = counter.length - 1; i >= 0; --i) {
11+
++res;
12+
if (counter[i] >= n) {
13+
return res;
14+
}
15+
n -= counter[i];
16+
}
17+
return res;
18+
}
19+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def halfQuestions(self, questions: List[int]) -> int:
3+
counter = collections.Counter(questions)
4+
counter = OrderedDict(counter.most_common())
5+
n = len(questions) >> 1
6+
res = 0
7+
for _, v in counter.items():
8+
res += 1
9+
if v >= n:
10+
return res
11+
n -= v
12+
return res

‎lcs/LCS 03. 主题空间/README.md

+135-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
> 输出:`1`
1616
>
1717
> 解释:4 个主题空间中,只有 1 个不与走廊相邻,面积为 1。
18-
> ![image.png](https://pic.leetcode-cn.com/1613708145-rscctN-image.png)
18+
> ![image.png](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcs/LCS%2003.%20主题空间/images/1613708145-rscctN-image.png)
1919
2020
**示例 2:**
2121

@@ -24,7 +24,7 @@
2424
> 输出:`3`
2525
>
2626
> 解释:8 个主题空间中,有 5 个不与走廊相邻,面积分别为 3、1、1、1、2,最大面积为 3。
27-
> ![image.png](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcs/LCS%2003.%20主题空间/images/613707985-KJyiXJ-image.png)
27+
> ![image.png](https://cdn.jsdelivr.net/gh/doocs/leetcode@main/lcs/LCS%2003.%20主题空间/images/1613707985-KJyiXJ-image.png)
2828
2929
**提示:**
3030

@@ -36,22 +36,153 @@
3636

3737
<!-- 这里可写通用的实现逻辑 -->
3838

39+
并查集。
40+
41+
并查集模板:
42+
43+
模板 1——朴素并查集:
44+
45+
```python
46+
# 初始化,p存储每个点的父节点
47+
p = list(range(n))
48+
49+
# 返回x的祖宗节点
50+
def find(x):
51+
if p[x] != x:
52+
# 路径压缩
53+
p[x] = find(p[x])
54+
return p[x]
55+
56+
# 合并a和b所在的两个集合
57+
p[find(a)] = find(b)
58+
```
59+
60+
模板 2——维护 size 的并查集:
61+
62+
```python
63+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
64+
p = list(range(n))
65+
size = [1] * n
66+
67+
# 返回x的祖宗节点
68+
def find(x):
69+
if p[x] != x:
70+
# 路径压缩
71+
p[x] = find(p[x])
72+
return p[x]
73+
74+
# 合并a和b所在的两个集合
75+
if find(a) != find(b):
76+
size[find(b)] += size[find(a)]
77+
p[find(a)] = find(b)
78+
```
79+
80+
模板 3——维护到祖宗节点距离的并查集:
81+
82+
```python
83+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
84+
p = list(range(n))
85+
d = [0] * n
86+
87+
# 返回x的祖宗节点
88+
def find(x):
89+
if p[x] != x:
90+
t = find(p[x])
91+
d[x] += d[p[x]]
92+
p[x] = t
93+
return p[x]
94+
95+
# 合并a和b所在的两个集合
96+
p[find(a)] = find(b)
97+
d[find(a)] = distance
98+
```
99+
39100
<!-- tabs:start -->
40101

41102
### **Python3**
42103

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

45106
```python
46-
107+
class Solution:
108+
def largestArea(self, grid: List[str]) -> int:
109+
m, n = len(grid), len(grid[0])
110+
p = list(range(m * n + 1))
111+
112+
def find(x):
113+
if p[x] != x:
114+
p[x] = find(p[x])
115+
return p[x]
116+
117+
for i in range(m):
118+
for j in range(n):
119+
if i == 0 or i == m - 1 or j == 0 or j == n - 1 or grid[i][j] == '0':
120+
p[find(i * n + j)] = find(m * n)
121+
else:
122+
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
123+
if grid[i + x][j + y] == '0' or grid[i][j] == grid[i + x][j + y]:
124+
p[find(i * n + j)] = find((i + x) * n + j + y)
125+
126+
mp = collections.defaultdict(int)
127+
res = 0
128+
for i in range(m):
129+
for j in range(n):
130+
root = find(i * n + j)
131+
if root != find(m * n):
132+
mp[root] += 1
133+
res = max(res, mp[root])
134+
return res
47135
```
48136

49137
### **Java**
50138

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

53141
```java
54-
142+
class Solution {
143+
private int[] p;
144+
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
145+
146+
public int largestArea(String[] grid) {
147+
int m = grid.length, n = grid[0].length();
148+
p = new int[m * n + 1];
149+
for (int i = 0; i < p.length; ++i) {
150+
p[i] = i;
151+
}
152+
for (int i = 0; i < m; ++i) {
153+
for (int j = 0; j < n; ++j) {
154+
if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i].charAt(j) == '0') {
155+
p[find(i * n + j)] = find(m * n);
156+
} else {
157+
for (int[] e : dirs) {
158+
if (grid[i + e[0]].charAt(j + e[1]) == '0' || grid[i].charAt(j) == grid[i + e[0]].charAt(j + e[1])) {
159+
p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
160+
}
161+
}
162+
}
163+
}
164+
}
165+
Map<Integer, Integer> mp = new HashMap<>();
166+
int res = 0;
167+
for (int i = 0; i < m; ++i) {
168+
for (int j = 0; j < n; ++j) {
169+
int root = find(i * n + j);
170+
if (root != find(m * n)) {
171+
mp.put(root, mp.getOrDefault(root, 0) + 1);
172+
res = Math.max(res, mp.get(root));
173+
}
174+
}
175+
}
176+
return res;
177+
}
178+
179+
private int find(int x) {
180+
if (p[x] != x) {
181+
p[x] = find(p[x]);
182+
}
183+
return p[x];
184+
}
185+
}
55186
```
56187

57188
### **...**
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution {
2+
private int[] p;
3+
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
4+
5+
public int largestArea(String[] grid) {
6+
int m = grid.length, n = grid[0].length();
7+
p = new int[m * n + 1];
8+
for (int i = 0; i < p.length; ++i) {
9+
p[i] = i;
10+
}
11+
for (int i = 0; i < m; ++i) {
12+
for (int j = 0; j < n; ++j) {
13+
if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i].charAt(j) == '0') {
14+
p[find(i * n + j)] = find(m * n);
15+
} else {
16+
for (int[] e : dirs) {
17+
if (grid[i + e[0]].charAt(j + e[1]) == '0' || grid[i].charAt(j) == grid[i + e[0]].charAt(j + e[1])) {
18+
p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
19+
}
20+
}
21+
}
22+
}
23+
}
24+
Map<Integer, Integer> mp = new HashMap<>();
25+
int res = 0;
26+
for (int i = 0; i < m; ++i) {
27+
for (int j = 0; j < n; ++j) {
28+
int root = find(i * n + j);
29+
if (root != find(m * n)) {
30+
mp.put(root, mp.getOrDefault(root, 0) + 1);
31+
res = Math.max(res, mp.get(root));
32+
}
33+
}
34+
}
35+
return res;
36+
}
37+
38+
private int find(int x) {
39+
if (p[x] != x) {
40+
p[x] = find(p[x]);
41+
}
42+
return p[x];
43+
}
44+
}

‎lcs/LCS 03. 主题空间/Solution.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def largestArea(self, grid: List[str]) -> int:
3+
m, n = len(grid), len(grid[0])
4+
p = list(range(m * n + 1))
5+
6+
def find(x):
7+
if p[x] != x:
8+
p[x] = find(p[x])
9+
return p[x]
10+
11+
for i in range(m):
12+
for j in range(n):
13+
if i == 0 or i == m - 1 or j == 0 or j == n - 1 or grid[i][j] == '0':
14+
p[find(i * n + j)] = find(m * n)
15+
else:
16+
for x, y in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
17+
if grid[i + x][j + y] == '0' or grid[i][j] == grid[i + x][j + y]:
18+
p[find(i * n + j)] = find((i + x) * n + j + y)
19+
20+
mp = collections.defaultdict(int)
21+
res = 0
22+
for i in range(m):
23+
for j in range(n):
24+
root = find(i * n + j)
25+
if root != find(m * n):
26+
mp[root] += 1
27+
res = max(res, mp[root])
28+
return res

0 commit comments

Comments
 (0)