Skip to content

Commit 6de67cb

Browse files
committed
feat: add solutions to lcs problems: No.01,02,03
1 parent ed4741d commit 6de67cb

File tree

11 files changed

+369
-1
lines changed

11 files changed

+369
-1
lines changed

lcs/LCS 01. 下载插件/README.md

+49-1
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,70 @@
4545

4646
<!-- 这里可写通用的实现逻辑 -->
4747

48+
如果不能在一分钟内下载完,那么可以先加速,循环直至能在一分钟内下载完。那么“循环次数 + 1”即为最少消耗的分钟数。
49+
4850
<!-- tabs:start -->
4951

5052
### **Python3**
5153

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

5456
```python
55-
57+
class Solution:
58+
def leastMinutes(self, n: int) -> int:
59+
speed = res = 1
60+
while speed < n:
61+
speed <<= 1
62+
res += 1
63+
return res
5664
```
5765

5866
### **Java**
5967

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

6270
```java
71+
class Solution {
72+
public int leastMinutes(int n) {
73+
int speed = 1;
74+
int res = 1;
75+
while (speed < n) {
76+
speed <<= 1;
77+
++res;
78+
}
79+
return res;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int leastMinutes(int n) {
90+
int speed = 1, res = 1;
91+
while (speed < n)
92+
{
93+
speed <<= 1;
94+
++res;
95+
}
96+
return res;
97+
}
98+
};
99+
```
63100
101+
### **Go**
102+
103+
```go
104+
func leastMinutes(n int) int {
105+
speed, res := 1, 1
106+
for speed < n {
107+
speed <<= 1
108+
res++
109+
}
110+
return res
111+
}
64112
```
65113

66114
### **...**

lcs/LCS 01. 下载插件/Solution.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public:
3+
int leastMinutes(int n) {
4+
int speed = 1, res = 1;
5+
while (speed < n)
6+
{
7+
speed <<= 1;
8+
++res;
9+
}
10+
return res;
11+
}
12+
};

lcs/LCS 01. 下载插件/Solution.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func leastMinutes(n int) int {
2+
speed, res := 1, 1
3+
for speed < n {
4+
speed <<= 1
5+
res++
6+
}
7+
return res
8+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public int leastMinutes(int n) {
3+
int speed = 1;
4+
int res = 1;
5+
while (speed < n) {
6+
speed <<= 1;
7+
++res;
8+
}
9+
return res;
10+
}
11+
}

lcs/LCS 01. 下载插件/Solution.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def leastMinutes(self, n: int) -> int:
3+
speed = res = 1
4+
while speed < n:
5+
speed <<= 1
6+
res += 1
7+
return res

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

+44
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,50 @@ class Solution {
8787
}
8888
```
8989

90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
int halfQuestions(vector<int>& questions) {
96+
vector<int> counter(1010);
97+
for (int e : questions) ++counter[e];
98+
int n = questions.size() >> 1;
99+
sort(counter.begin(), counter.end());
100+
int res = 0;
101+
for (int i = counter.size() - 1; i >= 0; --i)
102+
{
103+
++res;
104+
if (counter[i] >= n) return res;
105+
n -= counter[i];
106+
}
107+
return res;
108+
}
109+
};
110+
```
111+
112+
### **Go**
113+
114+
```go
115+
func halfQuestions(questions []int) int {
116+
counter := make([]int, 1010)
117+
for _, e := range questions {
118+
counter[e]++
119+
}
120+
n := len(questions) >> 1
121+
sort.Ints(counter)
122+
res := 0
123+
for i := len(counter) - 1; i >= 0; i-- {
124+
res++
125+
if counter[i] >= n {
126+
return res
127+
}
128+
n -= counter[i]
129+
}
130+
return res
131+
}
132+
```
133+
90134
### **...**
91135

92136
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int halfQuestions(vector<int>& questions) {
4+
vector<int> counter(1010);
5+
for (int e : questions) ++counter[e];
6+
int n = questions.size() >> 1;
7+
sort(counter.begin(), counter.end());
8+
int res = 0;
9+
for (int i = counter.size() - 1; i >= 0; --i)
10+
{
11+
++res;
12+
if (counter[i] >= n) return res;
13+
n -= counter[i];
14+
}
15+
return res;
16+
}
17+
};
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func halfQuestions(questions []int) int {
2+
counter := make([]int, 1010)
3+
for _, e := range questions {
4+
counter[e]++
5+
}
6+
n := len(questions) >> 1
7+
sort.Ints(counter)
8+
res := 0
9+
for i := len(counter) - 1; i >= 0; i-- {
10+
res++
11+
if counter[i] >= n {
12+
return res
13+
}
14+
n -= counter[i]
15+
}
16+
return res
17+
}

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

+107
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,113 @@ class Solution {
185185
}
186186
```
187187

188+
### **C++**
189+
190+
```cpp
191+
class Solution {
192+
public:
193+
vector<int> p;
194+
int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
195+
196+
int largestArea(vector<string>& grid) {
197+
int m = grid.size(), n = grid[0].size();
198+
p.resize(m * n + 1);
199+
for (int i = 0; i < p.size(); ++i) p[i] = i;
200+
for (int i = 0; i < m; ++i)
201+
{
202+
for (int j = 0; j < n; ++j)
203+
{
204+
if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i][j] == '0')
205+
p[find(i * n + j)] = find(m * n);
206+
else
207+
{
208+
for (auto e : dirs)
209+
{
210+
if (grid[i + e[0]][j + e[1]] == '0' || grid[i][j]== grid[i + e[0]][j + e[1]])
211+
p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
212+
}
213+
}
214+
}
215+
}
216+
unordered_map<int, int> mp;
217+
int res = 0;
218+
for (int i = 0; i < m; ++i)
219+
{
220+
for (int j = 0; j < n; ++j)
221+
{
222+
int root = find(i * n + j);
223+
if (root != find(m * n))
224+
{
225+
++mp[root];
226+
res = max(res, mp[root]);
227+
}
228+
}
229+
}
230+
return res;
231+
}
232+
233+
int find(int x) {
234+
if (p[x] != x) p[x] = find(p[x]);
235+
return p[x];
236+
}
237+
};
238+
```
239+
240+
### **Go**
241+
242+
```go
243+
var p []int
244+
245+
func largestArea(grid []string) int {
246+
m, n := len(grid), len(grid[0])
247+
p = make([]int, m*n+1)
248+
for i := 0; i < len(p); i++ {
249+
p[i] = i
250+
}
251+
252+
dirs := [4][2]int{{0, -1}, {0, 1}, {1, 0}, {-1, 0}}
253+
for i := 0; i < m; i++ {
254+
for j := 0; j < n; j++ {
255+
if i == 0 || i == m-1 || j == 0 || j == n-1 || grid[i][j] == '0' {
256+
p[find(i*n+j)] = find(m * n)
257+
} else {
258+
for _, e := range dirs {
259+
if grid[i+e[0]][j+e[1]] == '0' || grid[i][j] == grid[i+e[0]][j+e[1]] {
260+
p[find(i*n+j)] = find((i+e[0])*n + j + e[1])
261+
}
262+
}
263+
}
264+
}
265+
}
266+
mp := make(map[int]int, 0)
267+
res := 0
268+
for i := 0; i < m; i++ {
269+
for j := 0; j < n; j++ {
270+
root := find(i*n + j)
271+
if root != find(m*n) {
272+
mp[root]++
273+
res = max(res, mp[root])
274+
}
275+
}
276+
}
277+
return res
278+
}
279+
280+
func find(x int) int {
281+
if p[x] != x {
282+
p[x] = find(p[x])
283+
}
284+
return p[x]
285+
}
286+
287+
func max(a, b int) int {
288+
if a > b {
289+
return a
290+
}
291+
return b
292+
}
293+
```
294+
188295
### **...**
189296

190297
```

lcs/LCS 03. 主题空间/Solution.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public:
3+
vector<int> p;
4+
int dirs[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
5+
6+
int largestArea(vector<string>& grid) {
7+
int m = grid.size(), n = grid[0].size();
8+
p.resize(m * n + 1);
9+
for (int i = 0; i < p.size(); ++i) p[i] = i;
10+
for (int i = 0; i < m; ++i)
11+
{
12+
for (int j = 0; j < n; ++j)
13+
{
14+
if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i][j] == '0')
15+
p[find(i * n + j)] = find(m * n);
16+
else
17+
{
18+
for (auto e : dirs)
19+
{
20+
if (grid[i + e[0]][j + e[1]] == '0' || grid[i][j]== grid[i + e[0]][j + e[1]])
21+
p[find(i * n + j)] = find((i + e[0]) * n + j + e[1]);
22+
}
23+
}
24+
}
25+
}
26+
unordered_map<int, int> mp;
27+
int res = 0;
28+
for (int i = 0; i < m; ++i)
29+
{
30+
for (int j = 0; j < n; ++j)
31+
{
32+
int root = find(i * n + j);
33+
if (root != find(m * n))
34+
{
35+
++mp[root];
36+
res = max(res, mp[root]);
37+
}
38+
}
39+
}
40+
return res;
41+
}
42+
43+
int find(int x) {
44+
if (p[x] != x) p[x] = find(p[x]);
45+
return p[x];
46+
}
47+
};

0 commit comments

Comments
 (0)