mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 21:22:43 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ac4dc654fc | ||
|
|
273148c08b | ||
|
|
7960dee431 |
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 });
|
||||||
@@ -176,8 +176,8 @@ const topicRoutes = (nav: unknown): Record<string, string> => {
|
|||||||
// ── component ────────────────────────────────────────────────────
|
// ── component ────────────────────────────────────────────────────
|
||||||
|
|
||||||
interface ViewBox { x: number; y: number; w: number; h: number }
|
interface ViewBox { x: number; y: number; w: number; h: number }
|
||||||
/** The resting view — 2× on the centre so node labels read at the content width; 1× stays reachable by zooming out. */
|
/** The resting view — 1.6× on the centre so node labels read at the content width; 1× stays reachable by zooming out. */
|
||||||
const HOME_ZOOM = 2;
|
const HOME_ZOOM = 1.6;
|
||||||
const HOME: ViewBox = { x: (W - W / HOME_ZOOM) / 2, y: (H - H / HOME_ZOOM) / 2, w: W / HOME_ZOOM, h: H / HOME_ZOOM };
|
const HOME: ViewBox = { x: (W - W / HOME_ZOOM) / 2, y: (H - H / HOME_ZOOM) / 2, w: W / HOME_ZOOM, h: H / HOME_ZOOM };
|
||||||
const clamp = (v: number, lo: number, hi: number) => Math.min(Math.max(v, lo), hi);
|
const clamp = (v: number, lo: number, hi: number) => Math.min(Math.max(v, lo), hi);
|
||||||
|
|
||||||
|
|||||||
+69
@@ -0,0 +1,69 @@
|
|||||||
|
"""
|
||||||
|
323. Number of Connected Components in an Undirected Graph
|
||||||
|
Difficulty: Medium
|
||||||
|
https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/
|
||||||
|
|
||||||
|
──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Return the number of connected components in the graph.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
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,81 @@
|
|||||||
|
"""
|
||||||
|
684. Redundant Connection
|
||||||
|
Difficulty: Medium
|
||||||
|
https://leetcode.com/problems/redundant-connection/
|
||||||
|
|
||||||
|
──────────────────────────────────────────────────
|
||||||
|
|
||||||
|
In this problem, a tree is an undirected graph that is connected and
|
||||||
|
has no cycles.
|
||||||
|
|
||||||
|
You are given a graph that started as a tree with n nodes labeled
|
||||||
|
from 1 to n, with one additional edge added. The added edge has two
|
||||||
|
different vertices chosen from 1 to n, and was not an edge that
|
||||||
|
already existed. The graph is represented as an array edges of length
|
||||||
|
n where edges[i] = [ai, bi] indicates that there is an edge between
|
||||||
|
nodes ai and bi in the graph.
|
||||||
|
|
||||||
|
Return an edge that can be removed so that the resulting graph is a
|
||||||
|
tree of n nodes. If there are multiple answers, return the answer that
|
||||||
|
occurs last in the input.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
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]
|
||||||
Reference in new issue
Block a user