Skip to content

Commit

Permalink
222nd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 16, 2024
1 parent 904c529 commit 5806874
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ $ pnpm bench twoSum.bench.ts
- [二分搜尋 (Binary Search)](./algorithms/binary-search/README.md)
- 回溯 (Backtracking)
- 動態規劃 (Dynamic Programming)
- [0-1 背包問題 (0-1 Knapsack Problem)](./algorithms/dynamic-programming/README.md)

## Basic - LeetCode 75

Expand Down Expand Up @@ -267,12 +268,14 @@ Ace Coding Interview with 75 Qs

[17]: ./src/page-1/17.%20Letter%20Combinations%20of%20a%20Phone%20Number/letterCombinations.ts

| DP - 1D | | |
| ------------------------------ | -------- | ------ |
| 1137. N-th Tribonacci Number | Solution | Easy |
| 746. Min Cost Climbing Stairs | Solution | Easy |
| 198. House Robber | Solution | Medium |
| 790. Domino and Tromino Tiling | Solution | Medium |
| DP - 1D | | |
| ------------------------------ | ---------------- | ------ |
| 1137. N-th Tribonacci Number | [Solution][1137] | Easy |
| 746. Min Cost Climbing Stairs | Solution | Easy |
| 198. House Robber | Solution | Medium |
| 790. Domino and Tromino Tiling | Solution | Medium |

[1137]: ./src/page-11/1137.%20N-th%20Tribonacci%20Number/tribonacci.ts

| DP - Multidimensional | | |
| --------------------------------------------------------- | -------- | ------ |
Expand Down
63 changes: 63 additions & 0 deletions algorithms/dynamic-programming/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# 動態規劃 (Dynamic Programming)

## 0-1 背包問題 (0-1 Knapsack Problem)

問題:給定 n 個物品,每個物品有一定的重量和價值,並給定一個背包的容量。你需要在不超過背包容量的情況下選擇物品,使得所選物品的總價值最大。

- `Array(n)`: 物品數量
- `weights[]`: 物品的重量
- `values[]`: 物品的價值
- `capacity`: 背包的容量

```ts
function knapsack(weights: number[], values: number[], capacity: number): number {
const n = weights.length;

// 建立一個 n+1 橫行 (Row)、capacity+1 縱列 (Column) 的二維 DP 表,初始值都設為 0
const dp: number[][] = Array.from({ length: n + 1 }, () => Array(capacity + 1).fill(0));

for (let i = 1; i <= n; i++) {
for (let c = 0; c <= capacity; c++) {
if (weights[i - 1] <= c) {
// 遞推公式
dp[i][c] = Math.max(
dp[i - 1][c], // 不選擇物品
dp[i - 1][c - weights[i - 1]] + values[i - 1], // 選擇物品
);
} else {
dp[i][c] = dp[i - 1][c];
}
}
}

// dp[n][capacity] 即為考慮所有物品且容量為 capacity 時的最大價值
return dp[n][capacity];
}

const weights = [1, 2, 3];
const values = [5, 11, 15];
const capacity = 4;
console.log(knapsack(weights, values, capacity)); // 20
```

直接使用前一橫行 (Row) 的容量做優化:

```ts
function knapsack(weights: number[], values: number[], capacity: number): number {
const n = weights.length;
const dp: number[] = Array(capacity + 1).fill(0);

for (let i = 0; i < n; i++) {
for (let c = capacity; c >= weights[i]; c--) {
dp[c] = Math.max(dp[c], dp[c - weights[i]] + values[i]);
}
}

return dp[capacity];
}

const weights = [1, 2, 3];
const values = [5, 11, 15];
const capacity = 4;
console.log(knapsack(weights, values, capacity)); // 20
```
4 changes: 0 additions & 4 deletions src/page-11/1137. N-th Tribonacci Number/tribonacci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ type Tribonacci = (n: number) => number;
* Accepted
*/
export const tribonacci: Tribonacci = (n) => {
// Base cases
if (n === 0) return 0;
if (n === 1 || n === 2) return 1;

// Initialize the array with the first three values
const T = [0, 1, 1];

Expand Down

0 comments on commit 5806874

Please sign in to comment.