diff --git a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README.md b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README.md index d252ef7e74def..0ecd32b11bd2b 100644 --- a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README.md +++ b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README.md @@ -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$ 分别是节点数和边数。 @@ -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 = 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; +} +``` + diff --git a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README_EN.md b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README_EN.md index d90b69df177ce..1e83599b922b0 100644 --- a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README_EN.md +++ b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/README_EN.md @@ -87,7 +87,19 @@ tags: -### 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. @@ -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 = 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; +} +``` + diff --git a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/Solution.ts b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/Solution.ts new file mode 100644 index 0000000000000..510a1a7de751d --- /dev/null +++ b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/Solution.ts @@ -0,0 +1,37 @@ +function largestPathValue(colors: string, edges: number[][]): number { + const n = colors.length; + const indeg = Array(n).fill(0); + const g: Map = 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; +}