diff --git a/README.md b/README.md index 6e02f8b..5922e1d 100644 --- a/README.md +++ b/README.md @@ -46,8 +46,8 @@ $ pnpm test twoSum.test.ts 12. [分治 (Divide and Conquer)](./algorithms/12.divide-and-conquer/README.md) 13. [回溯 (Backtracking)](./algorithms/13.backtracking/README.md) 14. [動態規劃 (Dynamic Programming)](./algorithms/14.dynamic-programming/README.md) -15. 貪婪 (Greedy) -16. 位元操作 (Bit Manipulation) +15. [貪婪 (Greedy)](./algorithms/15.greedy/README.md) +16. [位元操作 (Bit Manipulation)](./algorithms/16.bit-manipulation/README.md) ## Basic - LeetCode 75 @@ -470,7 +470,7 @@ Must-do List for Interview Prep | Backtracking | | | | ----------------------------------------- | -------------- | ------ | -| 17. Letter Combinations of a Phone Number | Solution | Medium | +| 17. Letter Combinations of a Phone Number | [Solution][17] | Medium | | 77. Combinations | Solution | Medium | | 46. Permutations | [Solution][46] | Medium | | 39. Combination Sum | Solution | Medium | @@ -478,6 +478,7 @@ Must-do List for Interview Prep | 22. Generate Parentheses | Solution | Medium | | 79. Word Search | Solution | Medium | +[17]: ./src/page-1/17.%20Letter%20Combinations%20of%20a%20Phone%20Number/letterCombinations.ts [46]: ./src/page-1/46.%20Permutations/permute.ts | Divide & Conquer | | | diff --git a/algorithms/1.array/README.md b/algorithms/1.array/README.md index efdd1e6..238e047 100644 --- a/algorithms/1.array/README.md +++ b/algorithms/1.array/README.md @@ -138,3 +138,39 @@ for (let i = k; i < nums.length; i++) { ```ts maxSum; // 此時 `maxSum` 為最大和 ``` + +## 矩陣 (Matrix) + +```ts +const matrix = [ + [1, 2, 3], + [4, 5, 6], + [7, 8, 9], +]; +``` + +問題:將矩陣的橫行 (Row) 和縱列 (Column) 互換 + +取得矩陣 `rows` 和 `cols`: + +```ts +const rows = matrix.length; +const cols = matrix[0].length; +``` + +建立一個與原本矩陣橫行和縱列互換的空白新矩陣: + +```ts +const newMatrix = Array.from({ length: cols }, () => Array(rows)); +``` + +迴圈走訪原本矩陣的所有元素,將其分配到新矩陣上: + +```ts +for (let r = 0; r < rows; r++) { + for (let c = 0; c < cols; c++) { + // 橫行和縱列互換 + newMatrix[c][r] = matrix[r][c]; + } +} +``` diff --git a/algorithms/15.greedy/README.md b/algorithms/15.greedy/README.md index 8d98459..669c5ba 100644 --- a/algorithms/15.greedy/README.md +++ b/algorithms/15.greedy/README.md @@ -1,3 +1,41 @@ # 貪婪 (Greedy) ## 分數背包問題 (Fractional Knapsack Problem) + +給定一個背包,背包的容量是有限的,你可以選擇將物品裝入背包。每個物品有一個重量和價值,但你可以選擇將物品切割,部分裝入背包。目標是最大化裝入背包中的物品的總價值。 + +物品: + +```ts +const items = [ + { weight: 1, value: 5 }, + { weight: 2, value: 11 }, + { weight: 3, value: 15 }, +]; +``` + +單位價值:計算每個物品的單位重量價值 (價值/重量) + +每個物品的編號 (`i`) 及其對應的重量和價值: + +| i | weights[i - 1] | values[i - 1] | values[i - 1] / weights[i - 1] | +| --: | -------------: | ------------: | -----------------------------: | +| 1 | 1 | 5 | 5 | +| 2 | 2 | 11 | 5.5 | +| 3 | 3 | 15 | 5 | + +```ts +values[i - 1] / weights[i - 1]; +``` + +單位價值高低:根據單位價值將物品按從高到低排序 + +| i | weights[i - 1] | values[i - 1] | values[i - 1] / weights[i - 1] | +| --: | -------------: | ------------: | -----------------------------: | +| 2 | 2 | 11 | 5.5 | +| 1 | 1 | 5 | 5 | +| 3 | 3 | 15 | 5 | + +```ts +items.sort((a, b) => b.value / b.weight - a.value / a.weight); +``` diff --git a/algorithms/16.bit-manipulation/README.md b/algorithms/16.bit-manipulation/README.md index 4d106c5..8ab4789 100644 --- a/algorithms/16.bit-manipulation/README.md +++ b/algorithms/16.bit-manipulation/README.md @@ -1 +1,26 @@ # 位元操作 (Bit Manipulation) + +二進位制 + +- 0001 -> 1 +- 0010 -> 2 +- 0011 -> 3 +- 0100 -> 4 +- 0101 -> 5 +- 0110 -> 6 +- 0111 -> 7 +- 1000 -> 8 + +左移: + +```ts +const x = 1; // 0001 +x << 2; // 0100 -> 4 +``` + +右移: + +```ts +const x = 4; // 0100 +x >> 2; // 0001 -> 1 +``` diff --git a/src/page-1/67. Add Binary/addBinary.ts b/src/page-1/67. Add Binary/addBinary.ts index af74bf0..dc7c63b 100644 --- a/src/page-1/67. Add Binary/addBinary.ts +++ b/src/page-1/67. Add Binary/addBinary.ts @@ -4,5 +4,17 @@ type AddBinary = (a: string, b: string) => string; * Accepted */ export const addBinary: AddBinary = (a, b) => { - return (BigInt(`0b${a}`) + BigInt(`0b${b}`)).toString(2); + let x = BigInt(`0b${a}`); // Convert binary string 'a' to BigInt + let y = BigInt(`0b${b}`); // Convert binary string 'b' to BigInt + + let carry: bigint; + + while (y !== BigInt(0)) { + // Repeat until there's no carry + carry = x & y; // Calculate the carry + x = x ^ y; // Sum the bits without carry + y = carry << BigInt(1); // Move the carry to the left + } + + return x.toString(2); // Convert the final result back to a binary string };