Skip to content

Commit 182947e

Browse files
authored
Add TypeScript examples for chapters 10 and 11 (#311)
1 parent a9d98d8 commit 182947e

File tree

4 files changed

+127
-0
lines changed

4 files changed

+127
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// You pass an array in, and it gets converted to a set.
2+
let statesNeeded: Set<string> = new Set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]);
3+
4+
const stations: Record<string, Set<string>> = {};
5+
stations["kone"] = new Set(["id", "nv", "ut"]);
6+
stations["ktwo"] = new Set(["wa", "id", "mt"]);
7+
stations["kthree"] = new Set(["or", "nv", "ca"]);
8+
stations["kfour"] = new Set(["nv", "ut"]);
9+
stations["kfive"] = new Set(["ca", "az"]);
10+
11+
const finalStations: Set<string> = new Set();
12+
13+
while (statesNeeded.size) {
14+
let bestStation: string | null = null;
15+
let statesCovered: Set<string> = new Set();
16+
17+
for (let station in stations) {
18+
const states = stations[station];
19+
const covered = new Set([...statesNeeded].filter(x => states.has(x)));
20+
21+
if (covered.size > statesCovered.size) {
22+
bestStation = station;
23+
statesCovered = covered;
24+
}
25+
}
26+
27+
statesNeeded = new Set([...statesNeeded].filter(x => !statesCovered.has(x)));
28+
29+
if (bestStation !== null) {
30+
finalStations.add(bestStation);
31+
}
32+
}
33+
34+
console.log(finalStations); // Set { 'kone', 'ktwo', 'kthree', 'kfive' }

10_greedy_algorithms/ts/tsconfig.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"lib": [
4+
"dom",
5+
"es2015",
6+
"es2016",
7+
"es2017"
8+
],
9+
"target": "es2015"
10+
}
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Search for LCS
3+
*
4+
* @param {string} string1 first string
5+
* @param {string} string2 second string
6+
*
7+
* @return {object} with keys: lcs, offset, sequence
8+
*/
9+
function lcs(string1: string, string2: string): { lcs: number; offset: number; sequence: string } {
10+
if (typeof string1 !== "string" || typeof string2 !== "string" || !string1 || !string2) {
11+
return {
12+
lcs: 0,
13+
offset: 0,
14+
sequence: ""
15+
};
16+
}
17+
18+
let lcs = 0;
19+
let lastSubIndex = 0;
20+
21+
const table: number[][] = [];
22+
const len1 = string1.length;
23+
const len2 = string2.length;
24+
25+
let row: number;
26+
let col: number;
27+
28+
for (row = 0; row <= len1; row++) {
29+
table[row] = [];
30+
for (col = 0; col <= len2; col++) {
31+
table[row][col] = 0;
32+
}
33+
}
34+
35+
let i: number;
36+
let j: number;
37+
38+
for (i = 0; i < len1; i++) {
39+
for (j = 0; j < len2; j++) {
40+
if (string1[i] === string2[j]) {
41+
if (table[i][j] === 0) {
42+
table[i + 1][j + 1] = 1;
43+
} else {
44+
table[i + 1][j + 1] = table[i][j] + 1;
45+
}
46+
47+
if (table[i + 1][j + 1] > lcs) {
48+
lcs = table[i + 1][j + 1];
49+
lastSubIndex = i;
50+
}
51+
} else {
52+
table[i + 1][j + 1] = 0;
53+
}
54+
}
55+
}
56+
57+
return {
58+
lcs: lcs,
59+
offset: lastSubIndex - lcs + 1,
60+
sequence: string1.slice(lastSubIndex - lcs + 1, lastSubIndex + 1)
61+
};
62+
}
63+
64+
// Test cases
65+
console.log(lcs("hish", "fish")); // { lcs: 3, offset: 1, sequence: 'ish' }
66+
console.log(lcs("vista", "hish")); // { lcs: 2, offset: 1, sequence: 'is' }
67+
console.log(lcs("google", "abcdefgooglehijklm")); // { lcs: 6, offset: 0, sequence: 'google' }
68+
console.log(lcs("0", "0")); // { lcs: 1, offset: 0, sequence: '0' }
69+
console.log(lcs("0", 0 as any)); // { lcs: 0, offset: 0, sequence: '' }
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"lib": [
4+
"dom",
5+
"es2015",
6+
"es2016",
7+
"es2017"
8+
],
9+
"target": "es2015"
10+
}
11+
}
12+

0 commit comments

Comments
 (0)