Skip to content

feat: update lc problems #3674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tags:
<pre>
<strong>输入:</strong>nums = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
<strong>输出:</strong>[20,24]
<strong>解释:</strong>
<strong>解释:</strong>
列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。
列表 2:[0, 9, 12, 20],20 在区间 [20,24] 中。
列表 3:[5, 18, 22, 30],22 在区间 [20,24] 中。
Expand Down
4 changes: 2 additions & 2 deletions solution/0900-0999/0931.Minimum Falling Path Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tags:

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

<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>
<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>

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

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

<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>
<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>

<pre>
<strong>输入:</strong>matrix = [[-19,57],[-40,-5]]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion solution/1000-1099/1084.Sales Analysis III/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tags:

<!-- problem:start -->

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,21 @@ tags:
```python
class Solution:
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
def find(x):
def find(x: int) -> int:
if p[x] != x:
p[x] = find(p[x])
return p[x]

cnt, size = 0, n
cnt = 0
p = list(range(n))
for a, b in connections:
if find(a) == find(b):
pa, pb = find(a), find(b)
if pa == pb:
cnt += 1
else:
p[find(a)] = find(b)
size -= 1
return -1 if size - 1 > cnt else size - 1
p[pa] = pb
n -= 1
return -1 if n - 1 > cnt else n - 1
```

#### Java
Expand All @@ -117,12 +118,11 @@ class Solution {
}
int cnt = 0;
for (int[] e : connections) {
int a = e[0];
int b = e[1];
if (find(a) == find(b)) {
int pa = find(e[0]), pb = find(e[1]);
if (pa == pb) {
++cnt;
} else {
p[find(a)] = find(b);
p[pa] = pb;
--n;
}
}
Expand All @@ -143,27 +143,26 @@ class Solution {
```cpp
class Solution {
public:
vector<int> p;

int makeConnected(int n, vector<vector<int>>& connections) {
p.resize(n);
for (int i = 0; i < n; ++i) p[i] = i;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
int cnt = 0;
for (auto& e : connections) {
int a = e[0], b = e[1];
if (find(a) == find(b))
function<int(int)> find = [&](int x) -> int {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
};
for (const auto& c : connections) {
int pa = find(c[0]), pb = find(c[1]);
if (pa == pb) {
++cnt;
else {
p[find(a)] = find(b);
} else {
p[pa] = pb;
--n;
}
}
return n - 1 > cnt ? -1 : n - 1;
}

int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
return cnt >= n - 1 ? n - 1 : -1;
}
};
```
Expand All @@ -185,11 +184,11 @@ func makeConnected(n int, connections [][]int) int {
return p[x]
}
for _, e := range connections {
a, b := e[0], e[1]
if find(a) == find(b) {
pa, pb := find(e[0]), find(e[1])
if pa == pb {
cnt++
} else {
p[find(a)] = find(b)
p[pa] = pb
n--
}
}
Expand All @@ -200,6 +199,31 @@ func makeConnected(n int, connections [][]int) int {
}
```

#### TypeScript

```ts
function makeConnected(n: number, connections: number[][]): number {
const p: number[] = Array.from({ length: n }, (_, i) => i);
const find = (x: number): number => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};
let cnt = 0;
for (const [a, b] of connections) {
const [pa, pb] = [find(a), find(b)];
if (pa === pb) {
++cnt;
} else {
p[pa] = pb;
--n;
}
}
return cnt >= n - 1 ? n - 1 : -1;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,21 @@ tags:
```python
class Solution:
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
def find(x):
def find(x: int) -> int:
if p[x] != x:
p[x] = find(p[x])
return p[x]

cnt, size = 0, n
cnt = 0
p = list(range(n))
for a, b in connections:
if find(a) == find(b):
pa, pb = find(a), find(b)
if pa == pb:
cnt += 1
else:
p[find(a)] = find(b)
size -= 1
return -1 if size - 1 > cnt else size - 1
p[pa] = pb
n -= 1
return -1 if n - 1 > cnt else n - 1
```

#### Java
Expand All @@ -108,12 +109,11 @@ class Solution {
}
int cnt = 0;
for (int[] e : connections) {
int a = e[0];
int b = e[1];
if (find(a) == find(b)) {
int pa = find(e[0]), pb = find(e[1]);
if (pa == pb) {
++cnt;
} else {
p[find(a)] = find(b);
p[pa] = pb;
--n;
}
}
Expand All @@ -134,27 +134,26 @@ class Solution {
```cpp
class Solution {
public:
vector<int> p;

int makeConnected(int n, vector<vector<int>>& connections) {
p.resize(n);
for (int i = 0; i < n; ++i) p[i] = i;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
int cnt = 0;
for (auto& e : connections) {
int a = e[0], b = e[1];
if (find(a) == find(b))
function<int(int)> find = [&](int x) -> int {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
};
for (const auto& c : connections) {
int pa = find(c[0]), pb = find(c[1]);
if (pa == pb) {
++cnt;
else {
p[find(a)] = find(b);
} else {
p[pa] = pb;
--n;
}
}
return n - 1 > cnt ? -1 : n - 1;
}

int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
return cnt >= n - 1 ? n - 1 : -1;
}
};
```
Expand All @@ -176,11 +175,11 @@ func makeConnected(n int, connections [][]int) int {
return p[x]
}
for _, e := range connections {
a, b := e[0], e[1]
if find(a) == find(b) {
pa, pb := find(e[0]), find(e[1])
if pa == pb {
cnt++
} else {
p[find(a)] = find(b)
p[pa] = pb
n--
}
}
Expand All @@ -191,6 +190,31 @@ func makeConnected(n int, connections [][]int) int {
}
```

#### TypeScript

```ts
function makeConnected(n: number, connections: number[][]): number {
const p: number[] = Array.from({ length: n }, (_, i) => i);
const find = (x: number): number => {
if (p[x] !== x) {
p[x] = find(p[x]);
}
return p[x];
};
let cnt = 0;
for (const [a, b] of connections) {
const [pa, pb] = [find(a), find(b)];
if (pa === pb) {
++cnt;
} else {
p[pa] = pb;
--n;
}
}
return cnt >= n - 1 ? n - 1 : -1;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
class Solution {
public:
vector<int> p;

int makeConnected(int n, vector<vector<int>>& connections) {
p.resize(n);
for (int i = 0; i < n; ++i) p[i] = i;
vector<int> p(n);
iota(p.begin(), p.end(), 0);
int cnt = 0;
for (auto& e : connections) {
int a = e[0], b = e[1];
if (find(a) == find(b))
function<int(int)> find = [&](int x) -> int {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
};
for (const auto& c : connections) {
int pa = find(c[0]), pb = find(c[1]);
if (pa == pb) {
++cnt;
else {
p[find(a)] = find(b);
} else {
p[pa] = pb;
--n;
}
}
return n - 1 > cnt ? -1 : n - 1;
}

int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
return cnt >= n - 1 ? n - 1 : -1;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ func makeConnected(n int, connections [][]int) int {
return p[x]
}
for _, e := range connections {
a, b := e[0], e[1]
if find(a) == find(b) {
pa, pb := find(e[0]), find(e[1])
if pa == pb {
cnt++
} else {
p[find(a)] = find(b)
p[pa] = pb
n--
}
}
if n-1 > cnt {
return -1
}
return n - 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ public int makeConnected(int n, int[][] connections) {
}
int cnt = 0;
for (int[] e : connections) {
int a = e[0];
int b = e[1];
if (find(a) == find(b)) {
int pa = find(e[0]), pb = find(e[1]);
if (pa == pb) {
++cnt;
} else {
p[find(a)] = find(b);
p[pa] = pb;
--n;
}
}
Expand All @@ -26,4 +25,4 @@ private int find(int x) {
}
return p[x];
}
}
}
Loading
Loading