diff --git a/solution/2900-2999/2976.Minimum Cost to Convert String I/README.md b/solution/2900-2999/2976.Minimum Cost to Convert String I/README.md index 89e7133abb6a4..7809df863f835 100644 --- a/solution/2900-2999/2976.Minimum Cost to Convert String I/README.md +++ b/solution/2900-2999/2976.Minimum Cost to Convert String I/README.md @@ -53,8 +53,8 @@ tags: 输入:source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2] 输出:12 解释:要将字符 'a' 更改为 'b': -- 将字符 'a' 更改为 'c',成本为 1 -- 将字符 'c' 更改为 'b',成本为 2 +- 将字符 'a' 更改为 'c',成本为 1 +- 将字符 'c' 更改为 'b',成本为 2 产生的总成本是 1 + 2 = 3。 将所有 'a' 更改为 'b',产生的总成本是 3 * 4 = 12 。 @@ -276,39 +276,84 @@ function minimumCost( changed: string[], cost: number[], ): number { - const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity)); - for (let i = 0; i < 26; ++i) { - g[i][i] = 0; - } - for (let i = 0; i < original.length; ++i) { - let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0); - let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0); - let z: number = cost[i]; + const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY]; + const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(MAX)); + const getIndex = (ch: string) => ch.charCodeAt(0) - 'a'.charCodeAt(0); + + for (let i = 0; i < 26; ++i) g[i][i] = 0; + for (let i = 0; i < m; ++i) { + const x = getIndex(original[i]); + const y = getIndex(changed[i]); + const z = cost[i]; g[x][y] = Math.min(g[x][y], z); } for (let k = 0; k < 26; ++k) { for (let i = 0; i < 26; ++i) { - for (let j = 0; j < 26; ++j) { - g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + for (let j = 0; g[i][k] < MAX && j < 26; j++) { + if (g[k][j] < MAX) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } } } } - let ans: number = 0; - let n: number = source.length; + let ans = 0; for (let i = 0; i < n; ++i) { - let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0); - let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0); - if (x !== y) { - if (g[x][y] >= Infinity) { - return -1; + const x = getIndex(source[i]); + const y = getIndex(target[i]); + if (x === y) continue; + if (g[x][y] === MAX) return -1; + ans += g[x][y]; + } + return ans; +} +``` + +#### JavaScript + +```js +/** + * @param {string} source + * @param {string} target + * @param {character[]} original + * @param {character[]} changed + * @param {number[]} cost + * @return {number} + */ +var minimumCost = function (source, target, original, changed, cost) { + const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY]; + const g = Array.from({ length: 26 }, () => Array(26).fill(MAX)); + const getIndex = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0); + + for (let i = 0; i < 26; ++i) g[i][i] = 0; + for (let i = 0; i < m; ++i) { + const x = getIndex(original[i]); + const y = getIndex(changed[i]); + const z = cost[i]; + g[x][y] = Math.min(g[x][y], z); + } + + for (let k = 0; k < 26; ++k) { + for (let i = 0; i < 26; ++i) { + for (let j = 0; g[i][k] < MAX && j < 26; j++) { + if (g[k][j] < MAX) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } } - ans += g[x][y]; } } + + let ans = 0; + for (let i = 0; i < n; ++i) { + const x = getIndex(source[i]); + const y = getIndex(target[i]); + if (x === y) continue; + if (g[x][y] === MAX) return -1; + ans += g[x][y]; + } return ans; -} +}; ``` diff --git a/solution/2900-2999/2976.Minimum Cost to Convert String I/README_EN.md b/solution/2900-2999/2976.Minimum Cost to Convert String I/README_EN.md index 1575a16ef3d44..50fd6e6a57af3 100644 --- a/solution/2900-2999/2976.Minimum Cost to Convert String I/README_EN.md +++ b/solution/2900-2999/2976.Minimum Cost to Convert String I/README_EN.md @@ -268,39 +268,84 @@ function minimumCost( changed: string[], cost: number[], ): number { - const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity)); - for (let i = 0; i < 26; ++i) { - g[i][i] = 0; - } - for (let i = 0; i < original.length; ++i) { - let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0); - let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0); - let z: number = cost[i]; + const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY]; + const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(MAX)); + const getIndex = (ch: string) => ch.charCodeAt(0) - 'a'.charCodeAt(0); + + for (let i = 0; i < 26; ++i) g[i][i] = 0; + for (let i = 0; i < m; ++i) { + const x = getIndex(original[i]); + const y = getIndex(changed[i]); + const z = cost[i]; g[x][y] = Math.min(g[x][y], z); } for (let k = 0; k < 26; ++k) { for (let i = 0; i < 26; ++i) { - for (let j = 0; j < 26; ++j) { - g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + for (let j = 0; g[i][k] < MAX && j < 26; j++) { + if (g[k][j] < MAX) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } } } } - let ans: number = 0; - let n: number = source.length; + let ans = 0; for (let i = 0; i < n; ++i) { - let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0); - let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0); - if (x !== y) { - if (g[x][y] >= Infinity) { - return -1; + const x = getIndex(source[i]); + const y = getIndex(target[i]); + if (x === y) continue; + if (g[x][y] === MAX) return -1; + ans += g[x][y]; + } + return ans; +} +``` + +#### JavaScript + +```js +/** + * @param {string} source + * @param {string} target + * @param {character[]} original + * @param {character[]} changed + * @param {number[]} cost + * @return {number} + */ +var minimumCost = function (source, target, original, changed, cost) { + const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY]; + const g = Array.from({ length: 26 }, () => Array(26).fill(MAX)); + const getIndex = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0); + + for (let i = 0; i < 26; ++i) g[i][i] = 0; + for (let i = 0; i < m; ++i) { + const x = getIndex(original[i]); + const y = getIndex(changed[i]); + const z = cost[i]; + g[x][y] = Math.min(g[x][y], z); + } + + for (let k = 0; k < 26; ++k) { + for (let i = 0; i < 26; ++i) { + for (let j = 0; g[i][k] < MAX && j < 26; j++) { + if (g[k][j] < MAX) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } } - ans += g[x][y]; } } + + let ans = 0; + for (let i = 0; i < n; ++i) { + const x = getIndex(source[i]); + const y = getIndex(target[i]); + if (x === y) continue; + if (g[x][y] === MAX) return -1; + ans += g[x][y]; + } return ans; -} +}; ``` diff --git a/solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.js b/solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.js new file mode 100644 index 0000000000000..3a41902b0cc09 --- /dev/null +++ b/solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.js @@ -0,0 +1,41 @@ +/** + * @param {string} source + * @param {string} target + * @param {character[]} original + * @param {character[]} changed + * @param {number[]} cost + * @return {number} + */ +var minimumCost = function (source, target, original, changed, cost) { + const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY]; + const g = Array.from({ length: 26 }, () => Array(26).fill(MAX)); + const getIndex = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0); + + for (let i = 0; i < 26; ++i) g[i][i] = 0; + for (let i = 0; i < m; ++i) { + const x = getIndex(original[i]); + const y = getIndex(changed[i]); + const z = cost[i]; + g[x][y] = Math.min(g[x][y], z); + } + + for (let k = 0; k < 26; ++k) { + for (let i = 0; i < 26; ++i) { + for (let j = 0; g[i][k] < MAX && j < 26; j++) { + if (g[k][j] < MAX) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } + } + } + } + + let ans = 0; + for (let i = 0; i < n; ++i) { + const x = getIndex(source[i]); + const y = getIndex(target[i]); + if (x === y) continue; + if (g[x][y] === MAX) return -1; + ans += g[x][y]; + } + return ans; +}; diff --git a/solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.ts b/solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.ts index f9ae974e8d2c9..80cd3006dc965 100644 --- a/solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.ts +++ b/solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.ts @@ -5,36 +5,35 @@ function minimumCost( changed: string[], cost: number[], ): number { - const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity)); - for (let i = 0; i < 26; ++i) { - g[i][i] = 0; - } - for (let i = 0; i < original.length; ++i) { - let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0); - let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0); - let z: number = cost[i]; + const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY]; + const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(MAX)); + const getIndex = (ch: string) => ch.charCodeAt(0) - 'a'.charCodeAt(0); + + for (let i = 0; i < 26; ++i) g[i][i] = 0; + for (let i = 0; i < m; ++i) { + const x = getIndex(original[i]); + const y = getIndex(changed[i]); + const z = cost[i]; g[x][y] = Math.min(g[x][y], z); } for (let k = 0; k < 26; ++k) { for (let i = 0; i < 26; ++i) { - for (let j = 0; j < 26; ++j) { - g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + for (let j = 0; g[i][k] < MAX && j < 26; j++) { + if (g[k][j] < MAX) { + g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]); + } } } } - let ans: number = 0; - let n: number = source.length; + let ans = 0; for (let i = 0; i < n; ++i) { - let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0); - let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0); - if (x !== y) { - if (g[x][y] >= Infinity) { - return -1; - } - ans += g[x][y]; - } + const x = getIndex(source[i]); + const y = getIndex(target[i]); + if (x === y) continue; + if (g[x][y] === MAX) return -1; + ans += g[x][y]; } return ans; }