Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6cfdcc7

Browse files
authored
feat: update ts solution to lc problem: No.2976 (#3327)
1 parent e297bb8 commit 6cfdcc7

File tree

4 files changed

+190
-60
lines changed

4 files changed

+190
-60
lines changed

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

Lines changed: 66 additions & 21 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>
@@ -276,39 +276,84 @@ function minimumCost(
276276
changed: string[],
277277
cost: number[],
278278
): number {
279-
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity));
280-
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];
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];
287288
g[x][y] = Math.min(g[x][y], z);
288289
}
289290

290291
for (let k = 0; k < 26; ++k) {
291292
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]);
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]);
296+
}
294297
}
295298
}
296299
}
297300

298-
let ans: number = 0;
299-
let n: number = source.length;
301+
let ans = 0;
300302
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;
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;
310+
}
311+
```
312+
313+
#### JavaScript
314+
315+
```js
316+
/**
317+
* @param {string} source
318+
* @param {string} target
319+
* @param {character[]} original
320+
* @param {character[]} changed
321+
* @param {number[]} cost
322+
* @return {number}
323+
*/
324+
var minimumCost = function (source, target, original, changed, cost) {
325+
const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY];
326+
const g = Array.from({ length: 26 }, () => Array(26).fill(MAX));
327+
const getIndex = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0);
328+
329+
for (let i = 0; i < 26; ++i) g[i][i] = 0;
330+
for (let i = 0; i < m; ++i) {
331+
const x = getIndex(original[i]);
332+
const y = getIndex(changed[i]);
333+
const z = cost[i];
334+
g[x][y] = Math.min(g[x][y], z);
335+
}
336+
337+
for (let k = 0; k < 26; ++k) {
338+
for (let i = 0; i < 26; ++i) {
339+
for (let j = 0; g[i][k] < MAX && j < 26; j++) {
340+
if (g[k][j] < MAX) {
341+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
342+
}
306343
}
307-
ans += g[x][y];
308344
}
309345
}
346+
347+
let ans = 0;
348+
for (let i = 0; i < n; ++i) {
349+
const x = getIndex(source[i]);
350+
const y = getIndex(target[i]);
351+
if (x === y) continue;
352+
if (g[x][y] === MAX) return -1;
353+
ans += g[x][y];
354+
}
310355
return ans;
311-
}
356+
};
312357
```
313358

314359
<!-- tabs:end -->

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

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -268,39 +268,84 @@ function minimumCost(
268268
changed: string[],
269269
cost: number[],
270270
): number {
271-
const g: number[][] = Array.from({ length: 26 }, () => Array(26).fill(Infinity));
272-
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];
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];
279280
g[x][y] = Math.min(g[x][y], z);
280281
}
281282

282283
for (let k = 0; k < 26; ++k) {
283284
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]);
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]);
288+
}
286289
}
287290
}
288291
}
289292

290-
let ans: number = 0;
291-
let n: number = source.length;
293+
let ans = 0;
292294
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;
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;
302+
}
303+
```
304+
305+
#### JavaScript
306+
307+
```js
308+
/**
309+
* @param {string} source
310+
* @param {string} target
311+
* @param {character[]} original
312+
* @param {character[]} changed
313+
* @param {number[]} cost
314+
* @return {number}
315+
*/
316+
var minimumCost = function (source, target, original, changed, cost) {
317+
const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY];
318+
const g = Array.from({ length: 26 }, () => Array(26).fill(MAX));
319+
const getIndex = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0);
320+
321+
for (let i = 0; i < 26; ++i) g[i][i] = 0;
322+
for (let i = 0; i < m; ++i) {
323+
const x = getIndex(original[i]);
324+
const y = getIndex(changed[i]);
325+
const z = cost[i];
326+
g[x][y] = Math.min(g[x][y], z);
327+
}
328+
329+
for (let k = 0; k < 26; ++k) {
330+
for (let i = 0; i < 26; ++i) {
331+
for (let j = 0; g[i][k] < MAX && j < 26; j++) {
332+
if (g[k][j] < MAX) {
333+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
334+
}
298335
}
299-
ans += g[x][y];
300336
}
301337
}
338+
339+
let ans = 0;
340+
for (let i = 0; i < n; ++i) {
341+
const x = getIndex(source[i]);
342+
const y = getIndex(target[i]);
343+
if (x === y) continue;
344+
if (g[x][y] === MAX) return -1;
345+
ans += g[x][y];
346+
}
302347
return ans;
303-
}
348+
};
304349
```
305350

306351
<!-- tabs:end -->
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {string} source
3+
* @param {string} target
4+
* @param {character[]} original
5+
* @param {character[]} changed
6+
* @param {number[]} cost
7+
* @return {number}
8+
*/
9+
var minimumCost = function (source, target, original, changed, cost) {
10+
const [n, m, MAX] = [source.length, original.length, Number.POSITIVE_INFINITY];
11+
const g = Array.from({ length: 26 }, () => Array(26).fill(MAX));
12+
const getIndex = ch => ch.charCodeAt(0) - 'a'.charCodeAt(0);
13+
14+
for (let i = 0; i < 26; ++i) g[i][i] = 0;
15+
for (let i = 0; i < m; ++i) {
16+
const x = getIndex(original[i]);
17+
const y = getIndex(changed[i]);
18+
const z = cost[i];
19+
g[x][y] = Math.min(g[x][y], z);
20+
}
21+
22+
for (let k = 0; k < 26; ++k) {
23+
for (let i = 0; i < 26; ++i) {
24+
for (let j = 0; g[i][k] < MAX && j < 26; j++) {
25+
if (g[k][j] < MAX) {
26+
g[i][j] = Math.min(g[i][j], g[i][k] + g[k][j]);
27+
}
28+
}
29+
}
30+
}
31+
32+
let ans = 0;
33+
for (let i = 0; i < n; ++i) {
34+
const x = getIndex(source[i]);
35+
const y = getIndex(target[i]);
36+
if (x === y) continue;
37+
if (g[x][y] === MAX) return -1;
38+
ans += g[x][y];
39+
}
40+
return ans;
41+
};

‎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)

AltStyle によって変換されたページ (->オリジナル) /