Skip to content

Commit

Permalink
239th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 22, 2024
1 parent 058850a commit 7fcf57f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 4 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -470,14 +470,15 @@ 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 |
| 52. N-Queens II | Solution | Hard |
| 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 | | |
Expand Down
36 changes: 36 additions & 0 deletions algorithms/1.array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}
```
38 changes: 38 additions & 0 deletions algorithms/15.greedy/README.md
Original file line number Diff line number Diff line change
@@ -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);
```
25 changes: 25 additions & 0 deletions algorithms/16.bit-manipulation/README.md
Original file line number Diff line number Diff line change
@@ -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
```
14 changes: 13 additions & 1 deletion src/page-1/67. Add Binary/addBinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

0 comments on commit 7fcf57f

Please sign in to comment.