mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-04 05:37:38 +00:00
chore: clean up content phase organization
This commit is contained in:
1 parent
403a2d7f23
commit
bf0ac34a43
138 files changed
+1436
-368
No files matched your search
+58
@@ -0,0 +1,58 @@
|
||||
---
|
||||
title: '323. Number of Connected Components in an Undirected Graph'
|
||||
description: You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph
|
||||
sidebar:
|
||||
label: 'Number of Connected Components in an Undirected Graph'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
<Badge variant="accent">Depth-First Search</Badge>
|
||||
|
||||
### Example 1:
|
||||
- Input: `n = 5, edges = [[0,1],[1,2],[3,4]]`
|
||||
- Output: `2`
|
||||
|
||||
### Example 2:
|
||||
- Input: `n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]`
|
||||
- Output: `1`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `1 <= n <= 2000`
|
||||
- `1 <= edges.length <= 5000`
|
||||
- `edges[i]` = [ai, bi]
|
||||
- `ai != bi`
|
||||
- There are no repeated edges.
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
class Solution:
|
||||
def countComponents(self, n: int, edges: list[list[int]]) -> int:
|
||||
par = [i for i in range(n + 1)]
|
||||
rank = [1] * (n + 1)
|
||||
|
||||
def find(n):
|
||||
res = n
|
||||
while res != par[res]:
|
||||
par[res] = par[par[res]]
|
||||
res = par[res]
|
||||
return res
|
||||
|
||||
def union(n1, n2):
|
||||
p1, p2 = find(n1), find(n2)
|
||||
if p1 == p2:
|
||||
return 0
|
||||
if rank[p1] > rank[p2]:
|
||||
par[p2] = p1
|
||||
rank[p1] += rank[p2]
|
||||
else:
|
||||
par[p1] = p2
|
||||
rank[p2] += rank[p1]
|
||||
return 1
|
||||
|
||||
res = n
|
||||
for n1, n2 in edges:
|
||||
res -= union(n1, n2)
|
||||
return res
|
||||
```
|
||||
@@ -0,0 +1,60 @@
|
||||
---
|
||||
title: '684. Redundant Connection'
|
||||
description: In this problem, a tree is an undirected graph that is connected and has no cycles
|
||||
sidebar:
|
||||
label: 'Redundant Connection'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
<Badge variant="accent">Depth-First Search</Badge>
|
||||
|
||||
### Example 1:
|
||||
- Input: `edges = [[1,2],[1,3],[2,3]]`
|
||||
- Output: `[2,3]`
|
||||
|
||||
### Example 2:
|
||||
- Input: `edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]`
|
||||
- Output: `[1,4]`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- `n == edges.length`
|
||||
- `3 <= n <= 1000`
|
||||
- `edges[i].length == 2`
|
||||
- `1 <= ai < bi <= edges.length`
|
||||
- `ai != bi`
|
||||
- There are no repeated edges.
|
||||
- The given graph is connected.
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
class Solution:
|
||||
def findRedundantConnection(self, edges: list[list[int]]) -> list[int]:
|
||||
N = len(edges)
|
||||
par = [i for i in range(N + 1)]
|
||||
rank = [1] * (N + 1)
|
||||
|
||||
def find(n):
|
||||
if n != par[n]:
|
||||
par[n] = find(par[n])
|
||||
return par[n]
|
||||
|
||||
def union(n1, n2):
|
||||
p1, p2 = find(n1), find(n2)
|
||||
if p1 == p2:
|
||||
return False
|
||||
|
||||
if rank[p1] > rank[p2]:
|
||||
par[p2] = p1
|
||||
rank[p1] += rank[p2]
|
||||
else:
|
||||
par[p1] = p2
|
||||
rank[p2] += rank[p1]
|
||||
|
||||
return True
|
||||
|
||||
for n1, n2 in edges:
|
||||
if not union(n1, n2):
|
||||
return [n1, n2]
|
||||
```
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Union Find
|
||||
description: 'Union-Find (Disjoint Set Union) is a data structure that maintains a collection of disjoint sets and supports efficiently finding which set an element belongs to and merging two sets.'
|
||||
hidden: true
|
||||
---
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
import { defineMeta } from "blume";
|
||||
|
||||
export default defineMeta({ title: "Union-Find", order: 19, icon: "merge", display: "group" });
|
||||
Reference in new issue
Block a user