Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7210905

Browse files
feat: update lc problems (doocs#3674)
1 parent ae64403 commit 7210905

File tree

25 files changed

+181
-106
lines changed

25 files changed

+181
-106
lines changed

‎solution/0600-0699/0632.Smallest Range Covering Elements from K Lists/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ tags:
3232
<pre>
3333
<strong>输入:</strong>nums = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
3434
<strong>输出:</strong>[20,24]
35-
<strong>解释:</strong>
35+
<strong>解释:</strong>
3636
列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。
3737
列表 2:[0, 9, 12, 20],20 在区间 [20,24] 中。
3838
列表 3:[5, 18, 22, 30],22 在区间 [20,24] 中。

‎solution/0900-0999/0931.Minimum Falling Path Sum/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tags:
2626

2727
<p><strong>示例 1:</strong></p>
2828

29-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0931.Minimum%20Falling%20Path%20Sum/images/failing1-grid.jpg" style="height: 500px; width: 499px;" /></p>
29+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0931.Minimum%20Falling%20Path%20Sum/images/1729566253-aneDag-image.png" style="height: 500px; width: 499px;" /></p>
3030

3131
<pre>
3232
<strong>输入:</strong>matrix = [[2,1,3],[6,5,4],[7,8,9]]
@@ -36,7 +36,7 @@ tags:
3636

3737
<p><strong>示例 2:</strong></p>
3838

39-
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0931.Minimum%20Falling%20Path%20Sum/images/failing2-grid.jpg" style="height: 365px; width: 164px;" /></p>
39+
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0900-0999/0931.Minimum%20Falling%20Path%20Sum/images/1729566282-dtXwRd-image.png" style="height: 365px; width: 164px;" /></p>
4040

4141
<pre>
4242
<strong>输入:</strong>matrix = [[-19,57],[-40,-5]]
34.1 KB
Loading[フレーム]
15.7 KB
Loading[フレーム]

‎solution/1000-1099/1084.Sales Analysis III/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ tags:
88

99
<!-- problem:start -->
1010

11-
# [1084. 销售分析III](https://leetcode.cn/problems/sales-analysis-iii)
11+
# [1084. 销售分析 III](https://leetcode.cn/problems/sales-analysis-iii)
1212

1313
[English Version](/solution/1000-1099/1084.Sales%20Analysis%20III/README_EN.md)
1414

‎solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md‎

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,21 @@ tags:
8888
```python
8989
class Solution:
9090
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
91-
def find(x):
91+
def find(x: int) -> int:
9292
if p[x] != x:
9393
p[x] = find(p[x])
9494
return p[x]
9595

96-
cnt, size = 0, n
96+
cnt= 0
9797
p = list(range(n))
9898
for a, b in connections:
99-
if find(a) == find(b):
99+
pa, pb = find(a), find(b)
100+
if pa == pb:
100101
cnt += 1
101102
else:
102-
p[find(a)] = find(b)
103-
size -= 1
104-
return -1 if size - 1 > cnt else size - 1
103+
p[pa] = pb
104+
n -= 1
105+
return -1 if n - 1 > cnt else n - 1
105106
```
106107

107108
#### Java
@@ -117,12 +118,11 @@ class Solution {
117118
}
118119
int cnt = 0;
119120
for (int[] e : connections) {
120-
int a = e[0];
121-
int b = e[1];
122-
if (find(a) == find(b)) {
121+
int pa = find(e[0]), pb = find(e[1]);
122+
if (pa == pb) {
123123
++cnt;
124124
} else {
125-
p[find(a)] = find(b);
125+
p[pa] = pb;
126126
--n;
127127
}
128128
}
@@ -143,27 +143,26 @@ class Solution {
143143
```cpp
144144
class Solution {
145145
public:
146-
vector<int> p;
147-
148146
int makeConnected(int n, vector<vector<int>>& connections) {
149-
p.resize(n);
150-
for (int i = 0; i < n; ++i) p[i] = i;
147+
vector<int> p(n);
148+
iota(p.begin(), p.end(), 0);
151149
int cnt = 0;
152-
for (auto& e : connections) {
153-
int a = e[0], b = e[1];
154-
if (find(a) == find(b))
150+
function<int(int)> find = [&](int x) -> int {
151+
if (p[x] != x) {
152+
p[x] = find(p[x]);
153+
}
154+
return p[x];
155+
};
156+
for (const auto& c : connections) {
157+
int pa = find(c[0]), pb = find(c[1]);
158+
if (pa == pb) {
155159
++cnt;
156-
else {
157-
p[find(a)] = find(b);
160+
} else {
161+
p[pa] = pb;
158162
--n;
159163
}
160164
}
161-
return n - 1 > cnt ? -1 : n - 1;
162-
}
163-
164-
int find(int x) {
165-
if (p[x] != x) p[x] = find(p[x]);
166-
return p[x];
165+
return cnt >= n - 1 ? n - 1 : -1;
167166
}
168167
};
169168
```
@@ -185,11 +184,11 @@ func makeConnected(n int, connections [][]int) int {
185184
return p[x]
186185
}
187186
for _, e := range connections {
188-
a, b := e[0], e[1]
189-
if find(a) == find(b) {
187+
pa, pb := find(e[0]), find(e[1])
188+
if pa == pb {
190189
cnt++
191190
} else {
192-
p[find(a)] = find(b)
191+
p[pa] = pb
193192
n--
194193
}
195194
}
@@ -200,6 +199,31 @@ func makeConnected(n int, connections [][]int) int {
200199
}
201200
```
202201

202+
#### TypeScript
203+
204+
```ts
205+
function makeConnected(n: number, connections: number[][]): number {
206+
const p: number[] = Array.from({ length: n }, (_, i) => i);
207+
const find = (x: number): number => {
208+
if (p[x] !== x) {
209+
p[x] = find(p[x]);
210+
}
211+
return p[x];
212+
};
213+
let cnt = 0;
214+
for (const [a, b] of connections) {
215+
const [pa, pb] = [find(a), find(b)];
216+
if (pa === pb) {
217+
++cnt;
218+
} else {
219+
p[pa] = pb;
220+
--n;
221+
}
222+
}
223+
return cnt >= n - 1 ? n - 1 : -1;
224+
}
225+
```
226+
203227
<!-- tabs:end -->
204228

205229
<!-- solution:end -->

‎solution/1300-1399/1319.Number of Operations to Make Network Connected/README_EN.md‎

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,21 @@ tags:
7979
```python
8080
class Solution:
8181
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
82-
def find(x):
82+
def find(x: int) -> int:
8383
if p[x] != x:
8484
p[x] = find(p[x])
8585
return p[x]
8686

87-
cnt, size = 0, n
87+
cnt= 0
8888
p = list(range(n))
8989
for a, b in connections:
90-
if find(a) == find(b):
90+
pa, pb = find(a), find(b)
91+
if pa == pb:
9192
cnt += 1
9293
else:
93-
p[find(a)] = find(b)
94-
size -= 1
95-
return -1 if size - 1 > cnt else size - 1
94+
p[pa] = pb
95+
n -= 1
96+
return -1 if n - 1 > cnt else n - 1
9697
```
9798

9899
#### Java
@@ -108,12 +109,11 @@ class Solution {
108109
}
109110
int cnt = 0;
110111
for (int[] e : connections) {
111-
int a = e[0];
112-
int b = e[1];
113-
if (find(a) == find(b)) {
112+
int pa = find(e[0]), pb = find(e[1]);
113+
if (pa == pb) {
114114
++cnt;
115115
} else {
116-
p[find(a)] = find(b);
116+
p[pa] = pb;
117117
--n;
118118
}
119119
}
@@ -134,27 +134,26 @@ class Solution {
134134
```cpp
135135
class Solution {
136136
public:
137-
vector<int> p;
138-
139137
int makeConnected(int n, vector<vector<int>>& connections) {
140-
p.resize(n);
141-
for (int i = 0; i < n; ++i) p[i] = i;
138+
vector<int> p(n);
139+
iota(p.begin(), p.end(), 0);
142140
int cnt = 0;
143-
for (auto& e : connections) {
144-
int a = e[0], b = e[1];
145-
if (find(a) == find(b))
141+
function<int(int)> find = [&](int x) -> int {
142+
if (p[x] != x) {
143+
p[x] = find(p[x]);
144+
}
145+
return p[x];
146+
};
147+
for (const auto& c : connections) {
148+
int pa = find(c[0]), pb = find(c[1]);
149+
if (pa == pb) {
146150
++cnt;
147-
else {
148-
p[find(a)] = find(b);
151+
} else {
152+
p[pa] = pb;
149153
--n;
150154
}
151155
}
152-
return n - 1 > cnt ? -1 : n - 1;
153-
}
154-
155-
int find(int x) {
156-
if (p[x] != x) p[x] = find(p[x]);
157-
return p[x];
156+
return cnt >= n - 1 ? n - 1 : -1;
158157
}
159158
};
160159
```
@@ -176,11 +175,11 @@ func makeConnected(n int, connections [][]int) int {
176175
return p[x]
177176
}
178177
for _, e := range connections {
179-
a, b := e[0], e[1]
180-
if find(a) == find(b) {
178+
pa, pb := find(e[0]), find(e[1])
179+
if pa == pb {
181180
cnt++
182181
} else {
183-
p[find(a)] = find(b)
182+
p[pa] = pb
184183
n--
185184
}
186185
}
@@ -191,6 +190,31 @@ func makeConnected(n int, connections [][]int) int {
191190
}
192191
```
193192

193+
#### TypeScript
194+
195+
```ts
196+
function makeConnected(n: number, connections: number[][]): number {
197+
const p: number[] = Array.from({ length: n }, (_, i) => i);
198+
const find = (x: number): number => {
199+
if (p[x] !== x) {
200+
p[x] = find(p[x]);
201+
}
202+
return p[x];
203+
};
204+
let cnt = 0;
205+
for (const [a, b] of connections) {
206+
const [pa, pb] = [find(a), find(b)];
207+
if (pa === pb) {
208+
++cnt;
209+
} else {
210+
p[pa] = pb;
211+
--n;
212+
}
213+
}
214+
return cnt >= n - 1 ? n - 1 : -1;
215+
}
216+
```
217+
194218
<!-- tabs:end -->
195219

196220
<!-- solution:end -->
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
class Solution {
22
public:
3-
vector<int> p;
4-
53
int makeConnected(int n, vector<vector<int>>& connections) {
6-
p.resize(n);
7-
for (int i = 0; i < n; ++i) p[i] = i;
4+
vector<int> p(n);
5+
iota(p.begin(), p.end(), 0);
86
int cnt = 0;
9-
for (auto& e : connections) {
10-
int a = e[0], b = e[1];
11-
if (find(a) == find(b))
7+
function<int(int)> find = [&](int x) -> int {
8+
if (p[x] != x) {
9+
p[x] = find(p[x]);
10+
}
11+
return p[x];
12+
};
13+
for (const auto& c : connections) {
14+
int pa = find(c[0]), pb = find(c[1]);
15+
if (pa == pb) {
1216
++cnt;
13-
else {
14-
p[find(a)] = find(b);
17+
} else {
18+
p[pa] = pb;
1519
--n;
1620
}
1721
}
18-
return n - 1 > cnt ? -1 : n - 1;
19-
}
20-
21-
int find(int x) {
22-
if (p[x] != x) p[x] = find(p[x]);
23-
return p[x];
22+
return cnt >= n - 1 ? n - 1 : -1;
2423
}
25-
};
24+
};

‎solution/1300-1399/1319.Number of Operations to Make Network Connected/Solution.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ func makeConnected(n int, connections [][]int) int {
1212
return p[x]
1313
}
1414
for _, e := range connections {
15-
a, b := e[0], e[1]
16-
if find(a) == find(b) {
15+
pa, pb := find(e[0]), find(e[1])
16+
if pa == pb {
1717
cnt++
1818
} else {
19-
p[find(a)] = find(b)
19+
p[pa] = pb
2020
n--
2121
}
2222
}
2323
if n-1 > cnt {
2424
return -1
2525
}
2626
return n - 1
27-
}
27+
}

‎solution/1300-1399/1319.Number of Operations to Make Network Connected/Solution.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ public int makeConnected(int n, int[][] connections) {
88
}
99
int cnt = 0;
1010
for (int[] e : connections) {
11-
int a = e[0];
12-
int b = e[1];
13-
if (find(a) == find(b)) {
11+
int pa = find(e[0]), pb = find(e[1]);
12+
if (pa == pb) {
1413
++cnt;
1514
} else {
16-
p[find(a)] = find(b);
15+
p[pa] = pb;
1716
--n;
1817
}
1918
}
@@ -26,4 +25,4 @@ private int find(int x) {
2625
}
2726
return p[x];
2827
}
29-
}
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /