Skip to content

Commit 1440ec4

Browse files
committed
feat: add js solution to lc problem: No.2976
1 parent f5d2c67 commit 1440ec4

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,52 @@ export function minimumCost(
310310
}
311311
```
312312

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+
}
343+
}
344+
}
345+
}
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+
}
355+
return ans;
356+
};
357+
```
358+
313359
<!-- tabs:end -->
314360

315361
<!-- solution:end -->

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,52 @@ export function minimumCost(
302302
}
303303
```
304304

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+
}
335+
}
336+
}
337+
}
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+
}
347+
return ans;
348+
};
349+
```
350+
305351
<!-- tabs:end -->
306352

307353
<!-- solution: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+
};

0 commit comments

Comments
 (0)