From 182947e5fb0c5093eecc82ebf51b90297e5f78cf Mon Sep 17 00:00:00 2001 From: Igor Moraes Date: Thu, 1 May 2025 20:22:19 -0300 Subject: [PATCH] Add TypeScript examples for chapters 10 and 11 (#311) --- 10_greedy_algorithms/ts/01_set_covering.ts | 34 +++++++++ 10_greedy_algorithms/ts/tsconfig.json | 12 ++++ .../ts/01_longest_common_subsequence.ts | 69 +++++++++++++++++++ 11_dynamic_programming/ts/tsconfig.json | 12 ++++ 4 files changed, 127 insertions(+) create mode 100644 10_greedy_algorithms/ts/01_set_covering.ts create mode 100644 10_greedy_algorithms/ts/tsconfig.json create mode 100644 11_dynamic_programming/ts/01_longest_common_subsequence.ts create mode 100644 11_dynamic_programming/ts/tsconfig.json diff --git a/10_greedy_algorithms/ts/01_set_covering.ts b/10_greedy_algorithms/ts/01_set_covering.ts new file mode 100644 index 0000000..eb2e741 --- /dev/null +++ b/10_greedy_algorithms/ts/01_set_covering.ts @@ -0,0 +1,34 @@ +// You pass an array in, and it gets converted to a set. +let statesNeeded: Set = new Set(["mt", "wa", "or", "id", "nv", "ut", "ca", "az"]); + +const stations: Record> = {}; +stations["kone"] = new Set(["id", "nv", "ut"]); +stations["ktwo"] = new Set(["wa", "id", "mt"]); +stations["kthree"] = new Set(["or", "nv", "ca"]); +stations["kfour"] = new Set(["nv", "ut"]); +stations["kfive"] = new Set(["ca", "az"]); + +const finalStations: Set = new Set(); + +while (statesNeeded.size) { + let bestStation: string | null = null; + let statesCovered: Set = new Set(); + + for (let station in stations) { + const states = stations[station]; + const covered = new Set([...statesNeeded].filter(x => states.has(x))); + + if (covered.size > statesCovered.size) { + bestStation = station; + statesCovered = covered; + } + } + + statesNeeded = new Set([...statesNeeded].filter(x => !statesCovered.has(x))); + + if (bestStation !== null) { + finalStations.add(bestStation); + } +} + +console.log(finalStations); // Set { 'kone', 'ktwo', 'kthree', 'kfive' } \ No newline at end of file diff --git a/10_greedy_algorithms/ts/tsconfig.json b/10_greedy_algorithms/ts/tsconfig.json new file mode 100644 index 0000000..d4027c4 --- /dev/null +++ b/10_greedy_algorithms/ts/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "lib": [ + "dom", + "es2015", + "es2016", + "es2017" + ], + "target": "es2015" + } + } + \ No newline at end of file diff --git a/11_dynamic_programming/ts/01_longest_common_subsequence.ts b/11_dynamic_programming/ts/01_longest_common_subsequence.ts new file mode 100644 index 0000000..3483c09 --- /dev/null +++ b/11_dynamic_programming/ts/01_longest_common_subsequence.ts @@ -0,0 +1,69 @@ +/** + * Search for LCS + * + * @param {string} string1 first string + * @param {string} string2 second string + * + * @return {object} with keys: lcs, offset, sequence + */ +function lcs(string1: string, string2: string): { lcs: number; offset: number; sequence: string } { + if (typeof string1 !== "string" || typeof string2 !== "string" || !string1 || !string2) { + return { + lcs: 0, + offset: 0, + sequence: "" + }; + } + + let lcs = 0; + let lastSubIndex = 0; + + const table: number[][] = []; + const len1 = string1.length; + const len2 = string2.length; + + let row: number; + let col: number; + + for (row = 0; row <= len1; row++) { + table[row] = []; + for (col = 0; col <= len2; col++) { + table[row][col] = 0; + } + } + + let i: number; + let j: number; + + for (i = 0; i < len1; i++) { + for (j = 0; j < len2; j++) { + if (string1[i] === string2[j]) { + if (table[i][j] === 0) { + table[i + 1][j + 1] = 1; + } else { + table[i + 1][j + 1] = table[i][j] + 1; + } + + if (table[i + 1][j + 1] > lcs) { + lcs = table[i + 1][j + 1]; + lastSubIndex = i; + } + } else { + table[i + 1][j + 1] = 0; + } + } + } + + return { + lcs: lcs, + offset: lastSubIndex - lcs + 1, + sequence: string1.slice(lastSubIndex - lcs + 1, lastSubIndex + 1) + }; +} + +// Test cases +console.log(lcs("hish", "fish")); // { lcs: 3, offset: 1, sequence: 'ish' } +console.log(lcs("vista", "hish")); // { lcs: 2, offset: 1, sequence: 'is' } +console.log(lcs("google", "abcdefgooglehijklm")); // { lcs: 6, offset: 0, sequence: 'google' } +console.log(lcs("0", "0")); // { lcs: 1, offset: 0, sequence: '0' } +console.log(lcs("0", 0 as any)); // { lcs: 0, offset: 0, sequence: '' } \ No newline at end of file diff --git a/11_dynamic_programming/ts/tsconfig.json b/11_dynamic_programming/ts/tsconfig.json new file mode 100644 index 0000000..d4027c4 --- /dev/null +++ b/11_dynamic_programming/ts/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "lib": [ + "dom", + "es2015", + "es2016", + "es2017" + ], + "target": "es2015" + } + } + \ No newline at end of file