From f5d2c67a80d47cf4988a9a3772abb49efb3095f4 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sat, 27 Jul 2024 17:19:12 +0300 Subject: [PATCH 1/5] feat: update ts solution to lc problem: No.2976 --- .../README.md | 73 +++++++++---------- .../README_EN.md | 69 +++++++++--------- .../Solution.ts | 39 +++++----- 3 files changed, 89 insertions(+), 92 deletions(-) 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..abf2d941a5f52 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 。 @@ -269,45 +269,44 @@ func minimumCost(source string, target string, original []byte, changed []byte, #### TypeScript ```ts -function minimumCost( - source: string, - target: string, - original: string[], - changed: string[], - cost: number[], +export function minimumCost( + source: string, + target: string, + original: string[], + changed: string[], + cost: number[] ): number { - const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity)); + 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) { - 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]; - 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]); - } - } - } - - let ans: number = 0; - let n: number = source.length; - 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]; + 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]) } + } } - return ans; + } + + 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..a070685647b24 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 @@ -261,45 +261,44 @@ func minimumCost(source string, target string, original []byte, changed []byte, #### TypeScript ```ts -function minimumCost( - source: string, - target: string, - original: string[], - changed: string[], - cost: number[], +export function minimumCost( + source: string, + target: string, + original: string[], + changed: string[], + cost: number[] ): number { - const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity)); + 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) { - 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]; - 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]); - } - } - } - - let ans: number = 0; - let n: number = source.length; - 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]; + 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]) } + } } - return ans; + } + + 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; } From 1440ec4b0610f92047e50b407be688134b4f1528 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sat, 27 Jul 2024 17:21:48 +0300 Subject: [PATCH 2/5] feat: add js solution to lc problem: No.2976 --- .../README.md | 46 +++++++++++++++++++ .../README_EN.md | 46 +++++++++++++++++++ .../Solution.js | 41 +++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.js 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 abf2d941a5f52..cd3668705aa47 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 @@ -310,6 +310,52 @@ export function minimumCost( } ``` +#### 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]); + } + } + } + } + + 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 a070685647b24..71c91063933fa 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 @@ -302,6 +302,52 @@ export function minimumCost( } ``` +#### 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]); + } + } + } + } + + 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; +}; From 95f20e64254dc2ea98a258178e45d39a8bbc85e8 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sat, 27 Jul 2024 14:22:55 +0000 Subject: [PATCH 3/5] style: format code and docs with prettier --- .../README.md | 68 +++++++++---------- .../README_EN.md | 68 +++++++++---------- 2 files changed, 68 insertions(+), 68 deletions(-) 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 cd3668705aa47..9b86cd6334635 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 @@ -270,43 +270,43 @@ func minimumCost(source string, target string, original []byte, changed []byte, ```ts export function minimumCost( - source: string, - target: string, - original: string[], - changed: string[], - cost: number[] + source: string, + target: string, + original: string[], + changed: string[], + cost: number[], ): number { - 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; 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]) + 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; 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 + + 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 71c91063933fa..958f4e24b3b3c 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 @@ -262,43 +262,43 @@ func minimumCost(source string, target string, original []byte, changed []byte, ```ts export function minimumCost( - source: string, - target: string, - original: string[], - changed: string[], - cost: number[] + source: string, + target: string, + original: string[], + changed: string[], + cost: number[], ): number { - 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; 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]) + 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; 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 + + 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; } ``` From cf45f89acf67bae6dd6873016f84e1b274522925 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 3 Sep 2024 19:29:13 +0800 Subject: [PATCH 4/5] Update README.md --- .../2900-2999/2976.Minimum Cost to Convert String I/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9b86cd6334635..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 @@ -269,7 +269,7 @@ func minimumCost(source string, target string, original []byte, changed []byte, #### TypeScript ```ts -export function minimumCost( +function minimumCost( source: string, target: string, original: string[], From 1726248da60b1b6584d28409393ffd5658fb44ff Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 3 Sep 2024 19:29:31 +0800 Subject: [PATCH 5/5] Update README_EN.md --- .../2976.Minimum Cost to Convert String I/README_EN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 958f4e24b3b3c..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 @@ -261,7 +261,7 @@ func minimumCost(source string, target string, original []byte, changed []byte, #### TypeScript ```ts -export function minimumCost( +function minimumCost( source: string, target: string, original: string[],