Skip to content

feat: add solutions to lc problem: No.1857 #4432

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 1 commit into from
May 25, 2025
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 @@ -72,9 +72,17 @@ tags:

### 方法一:拓扑排序 + 动态规划

求出每个点的入度,进行拓扑排序。每个点维护一个长度为 $26$ 的数组,记录每个字母从任意起点到当前点的出现次数。
求出每个点的入度,进行拓扑排序。

时间复杂度 $O(n+m)$,空间复杂度 $O(n+m)$。
定义一个二维数组 $dp$,其中 $dp[i][j]$ 表示从起点到 $i$ 点,颜色为 $j$ 的节点数目。

从 $i$ 点出发,遍历所有出边 $i \to j$,更新 $dp[j][k] = \max(dp[j][k], dp[i][k] + (c == k))$,其中 $c$ 是 $j$ 点的颜色。

答案为数组 $dp$ 中的最大值。

如果图中有环,则无法遍历完所有点,返回 $-1$。

时间复杂度 $O((n + m) \times |\Sigma|)$,空间复杂度 $O(m + n \times |\Sigma)$。其中 $|\Sigma|$ 是字母表大小,这里为 $26$,而且 $n$ 和 $m$ 分别是节点数和边数。

<!-- tabs:start -->

Expand Down Expand Up @@ -252,6 +260,48 @@ func largestPathValue(colors string, edges [][]int) int {
}
```

#### TypeScript

```ts
function largestPathValue(colors: string, edges: number[][]): number {
const n = colors.length;
const indeg = Array(n).fill(0);
const g: Map<number, number[]> = new Map();
for (const [a, b] of edges) {
if (!g.has(a)) g.set(a, []);
g.get(a)!.push(b);
indeg[b]++;
}
const q: number[] = [];
const dp: number[][] = Array.from({ length: n }, () => Array(26).fill(0));
for (let i = 0; i < n; i++) {
if (indeg[i] === 0) {
q.push(i);
const c = colors.charCodeAt(i) - 97;
dp[i][c]++;
}
}
let cnt = 0;
let ans = 1;
while (q.length) {
const i = q.pop()!;
cnt++;
if (g.has(i)) {
for (const j of g.get(i)!) {
indeg[j]--;
if (indeg[j] === 0) q.push(j);
const c = colors.charCodeAt(j) - 97;
for (let k = 0; k < 26; k++) {
dp[j][k] = Math.max(dp[j][k], dp[i][k] + (c === k ? 1 : 0));
ans = Math.max(ans, dp[j][k]);
}
}
}
}
return cnt < n ? -1 : ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,19 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Topological Sort + Dynamic Programming

Calculate the in-degree of each node and perform a topological sort.

Define a 2D array $dp$, where $dp[i][j]$ represents the number of nodes with color $j$ on the path from the start node to node $i$.

From node $i$, traverse all outgoing edges $i \to j$, and update $dp[j][k] = \max(dp[j][k], dp[i][k] + (c == k))$, where $c$ is the color of node $j$.

The answer is the maximum value in the $dp$ array.

If there is a cycle in the graph, it is impossible to visit all nodes, so return $-1$.

The time complexity is $O((n + m) \times |\Sigma|)$, and the space complexity is $O(m + n \times |\Sigma|)$. Here, $|\Sigma|$ is the size of the alphabet (26 in this case), and $n$ and $m$ are the number of nodes and edges, respectively.

<!-- tabs:start -->

Expand Down Expand Up @@ -265,6 +277,48 @@ func largestPathValue(colors string, edges [][]int) int {
}
```

#### TypeScript

```ts
function largestPathValue(colors: string, edges: number[][]): number {
const n = colors.length;
const indeg = Array(n).fill(0);
const g: Map<number, number[]> = new Map();
for (const [a, b] of edges) {
if (!g.has(a)) g.set(a, []);
g.get(a)!.push(b);
indeg[b]++;
}
const q: number[] = [];
const dp: number[][] = Array.from({ length: n }, () => Array(26).fill(0));
for (let i = 0; i < n; i++) {
if (indeg[i] === 0) {
q.push(i);
const c = colors.charCodeAt(i) - 97;
dp[i][c]++;
}
}
let cnt = 0;
let ans = 1;
while (q.length) {
const i = q.pop()!;
cnt++;
if (g.has(i)) {
for (const j of g.get(i)!) {
indeg[j]--;
if (indeg[j] === 0) q.push(j);
const c = colors.charCodeAt(j) - 97;
for (let k = 0; k < 26; k++) {
dp[j][k] = Math.max(dp[j][k], dp[i][k] + (c === k ? 1 : 0));
ans = Math.max(ans, dp[j][k]);
}
}
}
}
return cnt < n ? -1 : ans;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function largestPathValue(colors: string, edges: number[][]): number {
const n = colors.length;
const indeg = Array(n).fill(0);
const g: Map<number, number[]> = new Map();
for (const [a, b] of edges) {
if (!g.has(a)) g.set(a, []);
g.get(a)!.push(b);
indeg[b]++;
}
const q: number[] = [];
const dp: number[][] = Array.from({ length: n }, () => Array(26).fill(0));
for (let i = 0; i < n; i++) {
if (indeg[i] === 0) {
q.push(i);
const c = colors.charCodeAt(i) - 97;
dp[i][c]++;
}
}
let cnt = 0;
let ans = 1;
while (q.length) {
const i = q.pop()!;
cnt++;
if (g.has(i)) {
for (const j of g.get(i)!) {
indeg[j]--;
if (indeg[j] === 0) q.push(j);
const c = colors.charCodeAt(j) - 97;
for (let k = 0; k < 26; k++) {
dp[j][k] = Math.max(dp[j][k], dp[i][k] + (c === k ? 1 : 0));
ans = Math.max(ans, dp[j][k]);
}
}
}
}
return cnt < n ? -1 : ans;
}