Skip to content

Commit f5d2c67

Browse files
committed
feat: update ts solution to lc problem: No.2976
1 parent 8228f7c commit f5d2c67

File tree

3 files changed

+89
-92
lines changed

3 files changed

+89
-92
lines changed

solution/2900-2999/2976.Minimum Cost to Convert String I/README.md

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ tags:
5353
<strong>输入:</strong>source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
5454
<strong>输出:</strong>12
5555
<strong>解释:</strong>要将字符 'a' 更改为 'b':
56-
- 将字符 'a' 更改为 'c',成本为 1
57-
- 将字符 'c' 更改为 'b',成本为 2
56+
- 将字符 'a' 更改为 'c',成本为 1
57+
- 将字符 'c' 更改为 'b',成本为 2
5858
产生的总成本是 1 + 2 = 3。
5959
将所有 'a' 更改为 'b',产生的总成本是 3 * 4 = 12 。
6060
</pre>
@@ -269,45 +269,44 @@ func minimumCost(source string, target string, original []byte, changed []byte,
269269
#### TypeScript
270270

271271
```ts
272-
function minimumCost(
273-
source: string,
274-
target: string,
275-
original: string[],
276-
changed: string[],
277-
cost: number[],
272+
export function minimumCost(
273+
source: string,
274+
target: string,
275+
original: string[],
276+
changed: string[],
277+
cost: number[]
278278
): number {
279-
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity));
279+
const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY]
280+
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(MAX))
281+
const getIndex = (ch: string) => ch.charCodeAt(0) - 'a'.charCodeAt(0)
282+
283+
for (let i = 0; i < 26; ++i) g[i][i] = 0
284+
for (let i = 0; i < m; ++i) {
285+
const x = getIndex(original[i])
286+
const y = getIndex(changed[i])
287+
const z = cost[i]
288+
g[x][y] = Math.min(g[x][y], z)
289+
}
290+
291+
for (let k = 0; k < 26; ++k) {
280292
for (let i = 0; i < 26; ++i) {
281-
g[i][i] = 0;
282-
}
283-
for (let i = 0; i < original.length; ++i) {
284-
let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0);
285-
let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0);
286-
let z: number = cost[i];
287-
g[x][y] = Math.min(g[x][y], z);
288-
}
289-
290-
for (let k = 0; k < 26; ++k) {
291-
for (let i = 0; i < 26; ++i) {
292-
for (let j = 0; j < 26; ++j) {
293-
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
294-
}
295-
}
296-
}
297-
298-
let ans: number = 0;
299-
let n: number = source.length;
300-
for (let i = 0; i < n; ++i) {
301-
let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0);
302-
let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0);
303-
if (x !== y) {
304-
if (g[x][y] >= Infinity) {
305-
return -1;
306-
}
307-
ans += g[x][y];
293+
for (let j = 0; g[i][k] < MAX && j < 26; j++) {
294+
if (g[k][j] < MAX) {
295+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j])
308296
}
297+
}
309298
}
310-
return ans;
299+
}
300+
301+
let ans = 0
302+
for (let i = 0; i < n; ++i) {
303+
const x = getIndex(source[i])
304+
const y = getIndex(target[i])
305+
if (x === y) continue
306+
if (g[x][y] === MAX) return -1
307+
ans += g[x][y]
308+
}
309+
return ans
311310
}
312311
```
313312

solution/2900-2999/2976.Minimum Cost to Convert String I/README_EN.md

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -261,45 +261,44 @@ func minimumCost(source string, target string, original []byte, changed []byte,
261261
#### TypeScript
262262

263263
```ts
264-
function minimumCost(
265-
source: string,
266-
target: string,
267-
original: string[],
268-
changed: string[],
269-
cost: number[],
264+
export function minimumCost(
265+
source: string,
266+
target: string,
267+
original: string[],
268+
changed: string[],
269+
cost: number[]
270270
): number {
271-
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity));
271+
const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY]
272+
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(MAX))
273+
const getIndex = (ch: string) => ch.charCodeAt(0) - 'a'.charCodeAt(0)
274+
275+
for (let i = 0; i < 26; ++i) g[i][i] = 0
276+
for (let i = 0; i < m; ++i) {
277+
const x = getIndex(original[i])
278+
const y = getIndex(changed[i])
279+
const z = cost[i]
280+
g[x][y] = Math.min(g[x][y], z)
281+
}
282+
283+
for (let k = 0; k < 26; ++k) {
272284
for (let i = 0; i < 26; ++i) {
273-
g[i][i] = 0;
274-
}
275-
for (let i = 0; i < original.length; ++i) {
276-
let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0);
277-
let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0);
278-
let z: number = cost[i];
279-
g[x][y] = Math.min(g[x][y], z);
280-
}
281-
282-
for (let k = 0; k < 26; ++k) {
283-
for (let i = 0; i < 26; ++i) {
284-
for (let j = 0; j < 26; ++j) {
285-
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
286-
}
287-
}
288-
}
289-
290-
let ans: number = 0;
291-
let n: number = source.length;
292-
for (let i = 0; i < n; ++i) {
293-
let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0);
294-
let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0);
295-
if (x !== y) {
296-
if (g[x][y] >= Infinity) {
297-
return -1;
298-
}
299-
ans += g[x][y];
285+
for (let j = 0; g[i][k] < MAX && j < 26; j++) {
286+
if (g[k][j] < MAX) {
287+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j])
300288
}
289+
}
301290
}
302-
return ans;
291+
}
292+
293+
let ans = 0
294+
for (let i = 0; i < n; ++i) {
295+
const x = getIndex(source[i])
296+
const y = getIndex(target[i])
297+
if (x === y) continue
298+
if (g[x][y] === MAX) return -1
299+
ans += g[x][y]
300+
}
301+
return ans
303302
}
304303
```
305304

solution/2900-2999/2976.Minimum Cost to Convert String I/Solution.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,35 @@ function minimumCost(
55
changed: string[],
66
cost: number[],
77
): number {
8-
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity));
9-
for (let i = 0; i < 26; ++i) {
10-
g[i][i] = 0;
11-
}
12-
for (let i = 0; i < original.length; ++i) {
13-
let x: number = original[i].charCodeAt(0) - 'a'.charCodeAt(0);
14-
let y: number = changed[i].charCodeAt(0) - 'a'.charCodeAt(0);
15-
let z: number = cost[i];
8+
const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY];
9+
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(MAX));
10+
const getIndex = (ch: string) => ch.charCodeAt(0) - 'a'.charCodeAt(0);
11+
12+
for (let i = 0; i < 26; ++i) g[i][i] = 0;
13+
for (let i = 0; i < m; ++i) {
14+
const x = getIndex(original[i]);
15+
const y = getIndex(changed[i]);
16+
const z = cost[i];
1617
g[x][y] = Math.min(g[x][y], z);
1718
}
1819

1920
for (let k = 0; k < 26; ++k) {
2021
for (let i = 0; i < 26; ++i) {
21-
for (let j = 0; j < 26; ++j) {
22-
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
22+
for (let j = 0; g[i][k] < MAX && j < 26; j++) {
23+
if (g[k][j] < MAX) {
24+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
25+
}
2326
}
2427
}
2528
}
2629

27-
let ans: number = 0;
28-
let n: number = source.length;
30+
let ans = 0;
2931
for (let i = 0; i < n; ++i) {
30-
let x: number = source.charCodeAt(i) - 'a'.charCodeAt(0);
31-
let y: number = target.charCodeAt(i) - 'a'.charCodeAt(0);
32-
if (x !== y) {
33-
if (g[x][y] >= Infinity) {
34-
return -1;
35-
}
36-
ans += g[x][y];
37-
}
32+
const x = getIndex(source[i]);
33+
const y = getIndex(target[i]);
34+
if (x === y) continue;
35+
if (g[x][y] === MAX) return -1;
36+
ans += g[x][y];
3837
}
3938
return ans;
4039
}

0 commit comments

Comments
 (0)