mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-02 06:23:12 +08:00
feat: Traditional Chinese version (#1163)
* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* File: climbing_stairs_backtrack.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_backtrack {
|
||||
/* 回溯 */
|
||||
void Backtrack(List<int> choices, int state, int n, List<int> res) {
|
||||
// 當爬到第 n 階時,方案數量加 1
|
||||
if (state == n)
|
||||
res[0]++;
|
||||
// 走訪所有選擇
|
||||
foreach (int choice in choices) {
|
||||
// 剪枝:不允許越過第 n 階
|
||||
if (state + choice > n)
|
||||
continue;
|
||||
// 嘗試:做出選擇,更新狀態
|
||||
Backtrack(choices, state + choice, n, res);
|
||||
// 回退
|
||||
}
|
||||
}
|
||||
|
||||
/* 爬樓梯:回溯 */
|
||||
int ClimbingStairsBacktrack(int n) {
|
||||
List<int> choices = [1, 2]; // 可選擇向上爬 1 階或 2 階
|
||||
int state = 0; // 從第 0 階開始爬
|
||||
List<int> res = [0]; // 使用 res[0] 記錄方案數量
|
||||
Backtrack(choices, state, n, res);
|
||||
return res[0];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
int res = ClimbingStairsBacktrack(n);
|
||||
Console.WriteLine($"爬 {n} 階樓梯共有 {res} 種方案");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* File: climbing_stairs_constraint_dp.cs
|
||||
* Created Time: 2023-07-03
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_constraint_dp {
|
||||
/* 帶約束爬樓梯:動態規劃 */
|
||||
int ClimbingStairsConstraintDP(int n) {
|
||||
if (n == 1 || n == 2) {
|
||||
return 1;
|
||||
}
|
||||
// 初始化 dp 表,用於儲存子問題的解
|
||||
int[,] dp = new int[n + 1, 3];
|
||||
// 初始狀態:預設最小子問題的解
|
||||
dp[1, 1] = 1;
|
||||
dp[1, 2] = 0;
|
||||
dp[2, 1] = 0;
|
||||
dp[2, 2] = 1;
|
||||
// 狀態轉移:從較小子問題逐步求解較大子問題
|
||||
for (int i = 3; i <= n; i++) {
|
||||
dp[i, 1] = dp[i - 1, 2];
|
||||
dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];
|
||||
}
|
||||
return dp[n, 1] + dp[n, 2];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
int res = ClimbingStairsConstraintDP(n);
|
||||
Console.WriteLine($"爬 {n} 階樓梯共有 {res} 種方案");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* File: climbing_stairs_dfs.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_dfs {
|
||||
/* 搜尋 */
|
||||
int DFS(int i) {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2)
|
||||
return i;
|
||||
// dp[i] = dp[i-1] + dp[i-2]
|
||||
int count = DFS(i - 1) + DFS(i - 2);
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 爬樓梯:搜尋 */
|
||||
int ClimbingStairsDFS(int n) {
|
||||
return DFS(n);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
int res = ClimbingStairsDFS(n);
|
||||
Console.WriteLine($"爬 {n} 階樓梯共有 {res} 種方案");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: climbing_stairs_dfs_mem.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_dfs_mem {
|
||||
/* 記憶化搜尋 */
|
||||
int DFS(int i, int[] mem) {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2)
|
||||
return i;
|
||||
// 若存在記錄 dp[i] ,則直接返回之
|
||||
if (mem[i] != -1)
|
||||
return mem[i];
|
||||
// dp[i] = dp[i-1] + dp[i-2]
|
||||
int count = DFS(i - 1, mem) + DFS(i - 2, mem);
|
||||
// 記錄 dp[i]
|
||||
mem[i] = count;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 爬樓梯:記憶化搜尋 */
|
||||
int ClimbingStairsDFSMem(int n) {
|
||||
// mem[i] 記錄爬到第 i 階的方案總數,-1 代表無記錄
|
||||
int[] mem = new int[n + 1];
|
||||
Array.Fill(mem, -1);
|
||||
return DFS(n, mem);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
int res = ClimbingStairsDFSMem(n);
|
||||
Console.WriteLine($"爬 {n} 階樓梯共有 {res} 種方案");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: climbing_stairs_dp.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class climbing_stairs_dp {
|
||||
/* 爬樓梯:動態規劃 */
|
||||
int ClimbingStairsDP(int n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
// 初始化 dp 表,用於儲存子問題的解
|
||||
int[] dp = new int[n + 1];
|
||||
// 初始狀態:預設最小子問題的解
|
||||
dp[1] = 1;
|
||||
dp[2] = 2;
|
||||
// 狀態轉移:從較小子問題逐步求解較大子問題
|
||||
for (int i = 3; i <= n; i++) {
|
||||
dp[i] = dp[i - 1] + dp[i - 2];
|
||||
}
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/* 爬樓梯:空間最佳化後的動態規劃 */
|
||||
int ClimbingStairsDPComp(int n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
int a = 1, b = 2;
|
||||
for (int i = 3; i <= n; i++) {
|
||||
int tmp = b;
|
||||
b = a + b;
|
||||
a = tmp;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int n = 9;
|
||||
|
||||
int res = ClimbingStairsDP(n);
|
||||
Console.WriteLine($"爬 {n} 階樓梯共有 {res} 種方案");
|
||||
|
||||
res = ClimbingStairsDPComp(n);
|
||||
Console.WriteLine($"爬 {n} 階樓梯共有 {res} 種方案");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* File: coin_change.cs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class coin_change {
|
||||
/* 零錢兌換:動態規劃 */
|
||||
int CoinChangeDP(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
int MAX = amt + 1;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n + 1, amt + 1];
|
||||
// 狀態轉移:首行首列
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
dp[0, a] = MAX;
|
||||
}
|
||||
// 狀態轉移:其餘行和列
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超過目標金額,則不選硬幣 i
|
||||
dp[i, a] = dp[i - 1, a];
|
||||
} else {
|
||||
// 不選和選硬幣 i 這兩種方案的較小值
|
||||
dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, amt] != MAX ? dp[n, amt] : -1;
|
||||
}
|
||||
|
||||
/* 零錢兌換:空間最佳化後的動態規劃 */
|
||||
int CoinChangeDPComp(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
int MAX = amt + 1;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[amt + 1];
|
||||
Array.Fill(dp, MAX);
|
||||
dp[0] = 0;
|
||||
// 狀態轉移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超過目標金額,則不選硬幣 i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// 不選和選硬幣 i 這兩種方案的較小值
|
||||
dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amt] != MAX ? dp[amt] : -1;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] coins = [1, 2, 5];
|
||||
int amt = 4;
|
||||
|
||||
// 動態規劃
|
||||
int res = CoinChangeDP(coins, amt);
|
||||
Console.WriteLine("湊到目標金額所需的最少硬幣數量為 " + res);
|
||||
|
||||
// 空間最佳化後的動態規劃
|
||||
res = CoinChangeDPComp(coins, amt);
|
||||
Console.WriteLine("湊到目標金額所需的最少硬幣數量為 " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* File: coin_change_ii.cs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class coin_change_ii {
|
||||
/* 零錢兌換 II:動態規劃 */
|
||||
int CoinChangeIIDP(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n + 1, amt + 1];
|
||||
// 初始化首列
|
||||
for (int i = 0; i <= n; i++) {
|
||||
dp[i, 0] = 1;
|
||||
}
|
||||
// 狀態轉移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超過目標金額,則不選硬幣 i
|
||||
dp[i, a] = dp[i - 1, a];
|
||||
} else {
|
||||
// 不選和選硬幣 i 這兩種方案之和
|
||||
dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, amt];
|
||||
}
|
||||
|
||||
/* 零錢兌換 II:空間最佳化後的動態規劃 */
|
||||
int CoinChangeIIDPComp(int[] coins, int amt) {
|
||||
int n = coins.Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[amt + 1];
|
||||
dp[0] = 1;
|
||||
// 狀態轉移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超過目標金額,則不選硬幣 i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// 不選和選硬幣 i 這兩種方案之和
|
||||
dp[a] = dp[a] + dp[a - coins[i - 1]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amt];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] coins = [1, 2, 5];
|
||||
int amt = 5;
|
||||
|
||||
// 動態規劃
|
||||
int res = CoinChangeIIDP(coins, amt);
|
||||
Console.WriteLine("湊出目標金額的硬幣組合數量為 " + res);
|
||||
|
||||
// 空間最佳化後的動態規劃
|
||||
res = CoinChangeIIDPComp(coins, amt);
|
||||
Console.WriteLine("湊出目標金額的硬幣組合數量為 " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/**
|
||||
* File: edit_distance.cs
|
||||
* Created Time: 2023-07-14
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class edit_distance {
|
||||
/* 編輯距離:暴力搜尋 */
|
||||
int EditDistanceDFS(string s, string t, int i, int j) {
|
||||
// 若 s 和 t 都為空,則返回 0
|
||||
if (i == 0 && j == 0)
|
||||
return 0;
|
||||
// 若 s 為空,則返回 t 長度
|
||||
if (i == 0)
|
||||
return j;
|
||||
// 若 t 為空,則返回 s 長度
|
||||
if (j == 0)
|
||||
return i;
|
||||
// 若兩字元相等,則直接跳過此兩字元
|
||||
if (s[i - 1] == t[j - 1])
|
||||
return EditDistanceDFS(s, t, i - 1, j - 1);
|
||||
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
|
||||
int insert = EditDistanceDFS(s, t, i, j - 1);
|
||||
int delete = EditDistanceDFS(s, t, i - 1, j);
|
||||
int replace = EditDistanceDFS(s, t, i - 1, j - 1);
|
||||
// 返回最少編輯步數
|
||||
return Math.Min(Math.Min(insert, delete), replace) + 1;
|
||||
}
|
||||
|
||||
/* 編輯距離:記憶化搜尋 */
|
||||
int EditDistanceDFSMem(string s, string t, int[][] mem, int i, int j) {
|
||||
// 若 s 和 t 都為空,則返回 0
|
||||
if (i == 0 && j == 0)
|
||||
return 0;
|
||||
// 若 s 為空,則返回 t 長度
|
||||
if (i == 0)
|
||||
return j;
|
||||
// 若 t 為空,則返回 s 長度
|
||||
if (j == 0)
|
||||
return i;
|
||||
// 若已有記錄,則直接返回之
|
||||
if (mem[i][j] != -1)
|
||||
return mem[i][j];
|
||||
// 若兩字元相等,則直接跳過此兩字元
|
||||
if (s[i - 1] == t[j - 1])
|
||||
return EditDistanceDFSMem(s, t, mem, i - 1, j - 1);
|
||||
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
|
||||
int insert = EditDistanceDFSMem(s, t, mem, i, j - 1);
|
||||
int delete = EditDistanceDFSMem(s, t, mem, i - 1, j);
|
||||
int replace = EditDistanceDFSMem(s, t, mem, i - 1, j - 1);
|
||||
// 記錄並返回最少編輯步數
|
||||
mem[i][j] = Math.Min(Math.Min(insert, delete), replace) + 1;
|
||||
return mem[i][j];
|
||||
}
|
||||
|
||||
/* 編輯距離:動態規劃 */
|
||||
int EditDistanceDP(string s, string t) {
|
||||
int n = s.Length, m = t.Length;
|
||||
int[,] dp = new int[n + 1, m + 1];
|
||||
// 狀態轉移:首行首列
|
||||
for (int i = 1; i <= n; i++) {
|
||||
dp[i, 0] = i;
|
||||
}
|
||||
for (int j = 1; j <= m; j++) {
|
||||
dp[0, j] = j;
|
||||
}
|
||||
// 狀態轉移:其餘行和列
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= m; j++) {
|
||||
if (s[i - 1] == t[j - 1]) {
|
||||
// 若兩字元相等,則直接跳過此兩字元
|
||||
dp[i, j] = dp[i - 1, j - 1];
|
||||
} else {
|
||||
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
|
||||
dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, m];
|
||||
}
|
||||
|
||||
/* 編輯距離:空間最佳化後的動態規劃 */
|
||||
int EditDistanceDPComp(string s, string t) {
|
||||
int n = s.Length, m = t.Length;
|
||||
int[] dp = new int[m + 1];
|
||||
// 狀態轉移:首行
|
||||
for (int j = 1; j <= m; j++) {
|
||||
dp[j] = j;
|
||||
}
|
||||
// 狀態轉移:其餘行
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// 狀態轉移:首列
|
||||
int leftup = dp[0]; // 暫存 dp[i-1, j-1]
|
||||
dp[0] = i;
|
||||
// 狀態轉移:其餘列
|
||||
for (int j = 1; j <= m; j++) {
|
||||
int temp = dp[j];
|
||||
if (s[i - 1] == t[j - 1]) {
|
||||
// 若兩字元相等,則直接跳過此兩字元
|
||||
dp[j] = leftup;
|
||||
} else {
|
||||
// 最少編輯步數 = 插入、刪除、替換這三種操作的最少編輯步數 + 1
|
||||
dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;
|
||||
}
|
||||
leftup = temp; // 更新為下一輪的 dp[i-1, j-1]
|
||||
}
|
||||
}
|
||||
return dp[m];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
string s = "bag";
|
||||
string t = "pack";
|
||||
int n = s.Length, m = t.Length;
|
||||
|
||||
// 暴力搜尋
|
||||
int res = EditDistanceDFS(s, t, n, m);
|
||||
Console.WriteLine("將 " + s + " 更改為 " + t + " 最少需要編輯 " + res + " 步");
|
||||
|
||||
// 記憶化搜尋
|
||||
int[][] mem = new int[n + 1][];
|
||||
for (int i = 0; i <= n; i++) {
|
||||
mem[i] = new int[m + 1];
|
||||
Array.Fill(mem[i], -1);
|
||||
}
|
||||
|
||||
res = EditDistanceDFSMem(s, t, mem, n, m);
|
||||
Console.WriteLine("將 " + s + " 更改為 " + t + " 最少需要編輯 " + res + " 步");
|
||||
|
||||
// 動態規劃
|
||||
res = EditDistanceDP(s, t);
|
||||
Console.WriteLine("將 " + s + " 更改為 " + t + " 最少需要編輯 " + res + " 步");
|
||||
|
||||
// 空間最佳化後的動態規劃
|
||||
res = EditDistanceDPComp(s, t);
|
||||
Console.WriteLine("將 " + s + " 更改為 " + t + " 最少需要編輯 " + res + " 步");
|
||||
}
|
||||
}
|
||||
118
zh-hant/codes/csharp/chapter_dynamic_programming/knapsack.cs
Normal file
118
zh-hant/codes/csharp/chapter_dynamic_programming/knapsack.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* File: knapsack.cs
|
||||
* Created Time: 2023-07-07
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class knapsack {
|
||||
/* 0-1 背包:暴力搜尋 */
|
||||
int KnapsackDFS(int[] weight, int[] val, int i, int c) {
|
||||
// 若已選完所有物品或背包無剩餘容量,則返回價值 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
}
|
||||
// 若超過背包容量,則只能選擇不放入背包
|
||||
if (weight[i - 1] > c) {
|
||||
return KnapsackDFS(weight, val, i - 1, c);
|
||||
}
|
||||
// 計算不放入和放入物品 i 的最大價值
|
||||
int no = KnapsackDFS(weight, val, i - 1, c);
|
||||
int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];
|
||||
// 返回兩種方案中價值更大的那一個
|
||||
return Math.Max(no, yes);
|
||||
}
|
||||
|
||||
/* 0-1 背包:記憶化搜尋 */
|
||||
int KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {
|
||||
// 若已選完所有物品或背包無剩餘容量,則返回價值 0
|
||||
if (i == 0 || c == 0) {
|
||||
return 0;
|
||||
}
|
||||
// 若已有記錄,則直接返回
|
||||
if (mem[i][c] != -1) {
|
||||
return mem[i][c];
|
||||
}
|
||||
// 若超過背包容量,則只能選擇不放入背包
|
||||
if (weight[i - 1] > c) {
|
||||
return KnapsackDFSMem(weight, val, mem, i - 1, c);
|
||||
}
|
||||
// 計算不放入和放入物品 i 的最大價值
|
||||
int no = KnapsackDFSMem(weight, val, mem, i - 1, c);
|
||||
int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];
|
||||
// 記錄並返回兩種方案中價值更大的那一個
|
||||
mem[i][c] = Math.Max(no, yes);
|
||||
return mem[i][c];
|
||||
}
|
||||
|
||||
/* 0-1 背包:動態規劃 */
|
||||
int KnapsackDP(int[] weight, int[] val, int cap) {
|
||||
int n = weight.Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n + 1, cap + 1];
|
||||
// 狀態轉移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (weight[i - 1] > c) {
|
||||
// 若超過背包容量,則不選物品 i
|
||||
dp[i, c] = dp[i - 1, c];
|
||||
} else {
|
||||
// 不選和選物品 i 這兩種方案的較大值
|
||||
dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, cap];
|
||||
}
|
||||
|
||||
/* 0-1 背包:空間最佳化後的動態規劃 */
|
||||
int KnapsackDPComp(int[] weight, int[] val, int cap) {
|
||||
int n = weight.Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[cap + 1];
|
||||
// 狀態轉移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// 倒序走訪
|
||||
for (int c = cap; c > 0; c--) {
|
||||
if (weight[i - 1] > c) {
|
||||
// 若超過背包容量,則不選物品 i
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// 不選和選物品 i 這兩種方案的較大值
|
||||
dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[cap];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] weight = [10, 20, 30, 40, 50];
|
||||
int[] val = [50, 120, 150, 210, 240];
|
||||
int cap = 50;
|
||||
int n = weight.Length;
|
||||
|
||||
// 暴力搜尋
|
||||
int res = KnapsackDFS(weight, val, n, cap);
|
||||
Console.WriteLine("不超過背包容量的最大物品價值為 " + res);
|
||||
|
||||
// 記憶化搜尋
|
||||
int[][] mem = new int[n + 1][];
|
||||
for (int i = 0; i <= n; i++) {
|
||||
mem[i] = new int[cap + 1];
|
||||
Array.Fill(mem[i], -1);
|
||||
}
|
||||
res = KnapsackDFSMem(weight, val, mem, n, cap);
|
||||
Console.WriteLine("不超過背包容量的最大物品價值為 " + res);
|
||||
|
||||
// 動態規劃
|
||||
res = KnapsackDP(weight, val, cap);
|
||||
Console.WriteLine("不超過背包容量的最大物品價值為 " + res);
|
||||
|
||||
// 空間最佳化後的動態規劃
|
||||
res = KnapsackDPComp(weight, val, cap);
|
||||
Console.WriteLine("不超過背包容量的最大物品價值為 " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* File: min_cost_climbing_stairs_dp.cs
|
||||
* Created Time: 2023-06-30
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class min_cost_climbing_stairs_dp {
|
||||
/* 爬樓梯最小代價:動態規劃 */
|
||||
int MinCostClimbingStairsDP(int[] cost) {
|
||||
int n = cost.Length - 1;
|
||||
if (n == 1 || n == 2)
|
||||
return cost[n];
|
||||
// 初始化 dp 表,用於儲存子問題的解
|
||||
int[] dp = new int[n + 1];
|
||||
// 初始狀態:預設最小子問題的解
|
||||
dp[1] = cost[1];
|
||||
dp[2] = cost[2];
|
||||
// 狀態轉移:從較小子問題逐步求解較大子問題
|
||||
for (int i = 3; i <= n; i++) {
|
||||
dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];
|
||||
}
|
||||
return dp[n];
|
||||
}
|
||||
|
||||
/* 爬樓梯最小代價:空間最佳化後的動態規劃 */
|
||||
int MinCostClimbingStairsDPComp(int[] cost) {
|
||||
int n = cost.Length - 1;
|
||||
if (n == 1 || n == 2)
|
||||
return cost[n];
|
||||
int a = cost[1], b = cost[2];
|
||||
for (int i = 3; i <= n; i++) {
|
||||
int tmp = b;
|
||||
b = Math.Min(a, tmp) + cost[i];
|
||||
a = tmp;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] cost = [0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1];
|
||||
Console.WriteLine("輸入樓梯的代價串列為");
|
||||
PrintUtil.PrintList(cost);
|
||||
|
||||
int res = MinCostClimbingStairsDP(cost);
|
||||
Console.WriteLine($"爬完樓梯的最低代價為 {res}");
|
||||
|
||||
res = MinCostClimbingStairsDPComp(cost);
|
||||
Console.WriteLine($"爬完樓梯的最低代價為 {res}");
|
||||
}
|
||||
}
|
||||
127
zh-hant/codes/csharp/chapter_dynamic_programming/min_path_sum.cs
Normal file
127
zh-hant/codes/csharp/chapter_dynamic_programming/min_path_sum.cs
Normal file
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* File: min_path_sum.cs
|
||||
* Created Time: 2023-07-10
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class min_path_sum {
|
||||
/* 最小路徑和:暴力搜尋 */
|
||||
int MinPathSumDFS(int[][] grid, int i, int j) {
|
||||
// 若為左上角單元格,則終止搜尋
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
}
|
||||
// 若行列索引越界,則返回 +∞ 代價
|
||||
if (i < 0 || j < 0) {
|
||||
return int.MaxValue;
|
||||
}
|
||||
// 計算從左上角到 (i-1, j) 和 (i, j-1) 的最小路徑代價
|
||||
int up = MinPathSumDFS(grid, i - 1, j);
|
||||
int left = MinPathSumDFS(grid, i, j - 1);
|
||||
// 返回從左上角到 (i, j) 的最小路徑代價
|
||||
return Math.Min(left, up) + grid[i][j];
|
||||
}
|
||||
|
||||
/* 最小路徑和:記憶化搜尋 */
|
||||
int MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {
|
||||
// 若為左上角單元格,則終止搜尋
|
||||
if (i == 0 && j == 0) {
|
||||
return grid[0][0];
|
||||
}
|
||||
// 若行列索引越界,則返回 +∞ 代價
|
||||
if (i < 0 || j < 0) {
|
||||
return int.MaxValue;
|
||||
}
|
||||
// 若已有記錄,則直接返回
|
||||
if (mem[i][j] != -1) {
|
||||
return mem[i][j];
|
||||
}
|
||||
// 左邊和上邊單元格的最小路徑代價
|
||||
int up = MinPathSumDFSMem(grid, mem, i - 1, j);
|
||||
int left = MinPathSumDFSMem(grid, mem, i, j - 1);
|
||||
// 記錄並返回左上角到 (i, j) 的最小路徑代價
|
||||
mem[i][j] = Math.Min(left, up) + grid[i][j];
|
||||
return mem[i][j];
|
||||
}
|
||||
|
||||
/* 最小路徑和:動態規劃 */
|
||||
int MinPathSumDP(int[][] grid) {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n, m];
|
||||
dp[0, 0] = grid[0][0];
|
||||
// 狀態轉移:首行
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[0, j] = dp[0, j - 1] + grid[0][j];
|
||||
}
|
||||
// 狀態轉移:首列
|
||||
for (int i = 1; i < n; i++) {
|
||||
dp[i, 0] = dp[i - 1, 0] + grid[i][0];
|
||||
}
|
||||
// 狀態轉移:其餘行和列
|
||||
for (int i = 1; i < n; i++) {
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];
|
||||
}
|
||||
}
|
||||
return dp[n - 1, m - 1];
|
||||
}
|
||||
|
||||
/* 最小路徑和:空間最佳化後的動態規劃 */
|
||||
int MinPathSumDPComp(int[][] grid) {
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[m];
|
||||
dp[0] = grid[0][0];
|
||||
// 狀態轉移:首行
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[j] = dp[j - 1] + grid[0][j];
|
||||
}
|
||||
// 狀態轉移:其餘行
|
||||
for (int i = 1; i < n; i++) {
|
||||
// 狀態轉移:首列
|
||||
dp[0] = dp[0] + grid[i][0];
|
||||
// 狀態轉移:其餘列
|
||||
for (int j = 1; j < m; j++) {
|
||||
dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];
|
||||
}
|
||||
}
|
||||
return dp[m - 1];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[][] grid =
|
||||
[
|
||||
[1, 3, 1, 5],
|
||||
[2, 2, 4, 2],
|
||||
[5, 3, 2, 1],
|
||||
[4, 3, 5, 2]
|
||||
];
|
||||
|
||||
int n = grid.Length, m = grid[0].Length;
|
||||
|
||||
// 暴力搜尋
|
||||
int res = MinPathSumDFS(grid, n - 1, m - 1);
|
||||
Console.WriteLine("從左上角到右下角的做小路徑和為 " + res);
|
||||
|
||||
// 記憶化搜尋
|
||||
int[][] mem = new int[n][];
|
||||
for (int i = 0; i < n; i++) {
|
||||
mem[i] = new int[m];
|
||||
Array.Fill(mem[i], -1);
|
||||
}
|
||||
res = MinPathSumDFSMem(grid, mem, n - 1, m - 1);
|
||||
Console.WriteLine("從左上角到右下角的做小路徑和為 " + res);
|
||||
|
||||
// 動態規劃
|
||||
res = MinPathSumDP(grid);
|
||||
Console.WriteLine("從左上角到右下角的做小路徑和為 " + res);
|
||||
|
||||
// 空間最佳化後的動態規劃
|
||||
res = MinPathSumDPComp(grid);
|
||||
Console.WriteLine("從左上角到右下角的做小路徑和為 " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* File: unbounded_knapsack.cs
|
||||
* Created Time: 2023-07-12
|
||||
* Author: hpstory (hpstory1024@163.com)
|
||||
*/
|
||||
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* 完全背包:動態規劃 */
|
||||
int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// 初始化 dp 表
|
||||
int[,] dp = new int[n + 1, cap + 1];
|
||||
// 狀態轉移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// 若超過背包容量,則不選物品 i
|
||||
dp[i, c] = dp[i - 1, c];
|
||||
} else {
|
||||
// 不選和選物品 i 這兩種方案的較大值
|
||||
dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n, cap];
|
||||
}
|
||||
|
||||
/* 完全背包:空間最佳化後的動態規劃 */
|
||||
int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[cap + 1];
|
||||
// 狀態轉移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// 若超過背包容量,則不選物品 i
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// 不選和選物品 i 這兩種方案的較大值
|
||||
dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[cap];
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
int[] wgt = [1, 2, 3];
|
||||
int[] val = [5, 11, 15];
|
||||
int cap = 4;
|
||||
|
||||
// 動態規劃
|
||||
int res = UnboundedKnapsackDP(wgt, val, cap);
|
||||
Console.WriteLine("不超過背包容量的最大物品價值為 " + res);
|
||||
|
||||
// 空間最佳化後的動態規劃
|
||||
res = UnboundedKnapsackDPComp(wgt, val, cap);
|
||||
Console.WriteLine("不超過背包容量的最大物品價值為 " + res);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user