From bf1bccc1ae5a9a7170b616d2522981b19f54572b Mon Sep 17 00:00:00 2001 From: hpstory <33348162+hpstory@users.noreply.github.com> Date: Mon, 3 Jul 2023 16:44:43 +0800 Subject: [PATCH] feat(csharp): add csharp code for charper dynamic programming (#574) * feat(csharp): add csharp code for charper dynamic programming * add climbing_stairs_constraint_dp --- .../climbing_stairs_backtrack.cs | 41 ++++++++++++++ .../climbing_stairs_constraint_dp.cs | 36 +++++++++++++ .../climbing_stairs_dfs.cs | 31 +++++++++++ .../climbing_stairs_dfs_mem.cs | 39 ++++++++++++++ .../climbing_stairs_dp.cs | 49 +++++++++++++++++ .../min_cost_climbing_stairs_dp.cs | 53 +++++++++++++++++++ codes/csharp/utils/PrintUtil.cs | 2 +- 7 files changed, 250 insertions(+), 1 deletion(-) create mode 100644 codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs create mode 100644 codes/csharp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cs create mode 100644 codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs.cs create mode 100644 codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs_mem.cs create mode 100644 codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs create mode 100644 codes/csharp/chapter_dynamic_programming/min_cost_climbing_stairs_dp.cs diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs new file mode 100644 index 000000000..88f8c028a --- /dev/null +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_backtrack.cs @@ -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 { + /* 回溯 */ + public void backtrack(List choices, int state, int n, List res) { + // 当爬到第 n 阶时,方案数量加 1 + if (state == n) + res[0]++; + // 遍历所有选择 + foreach (int choice in choices) { + // 剪枝:不允许越过第 n 阶 + if (state + choice > n) + break; + // 尝试:做出选择,更新状态 + backtrack(choices, state + choice, n, res); + // 回退 + } + } + + /* 爬楼梯:回溯 */ + public int climbingStairsBacktrack(int n) { + List choices = new List { 1, 2 }; // 可选择向上爬 1 或 2 阶 + int state = 0; // 从第 0 阶开始爬 + List res = new List { 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} 种方案"); + } +} diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cs new file mode 100644 index 000000000..2e6242c05 --- /dev/null +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_constraint_dp.cs @@ -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 { + /* 带约束爬楼梯:动态规划 */ + public int climbingStairsConstraintDP(int n) { + if (n == 1 || n == 2) { + return n; + } + // 初始化 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} 种方案"); + } +} diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs.cs new file mode 100644 index 000000000..29dd91940 --- /dev/null +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs.cs @@ -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 { + /* 搜索 */ + public 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; + } + + /* 爬楼梯:搜索 */ + public int climbingStairsDFS(int n) { + return dfs(n); + } + + [Test] + public void Test() { + int n = 9; + int res = climbingStairsDFS(n); + Console.WriteLine($"爬 {n} 阶楼梯共有 {res} 种方案"); + } +} diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs_mem.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs_mem.cs new file mode 100644 index 000000000..fdc9bad40 --- /dev/null +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dfs_mem.cs @@ -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 { + /* 记忆化搜索 */ + public 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; + } + + /* 爬楼梯:记忆化搜索 */ + public 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} 种方案"); + } +} diff --git a/codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs new file mode 100644 index 000000000..1e7347e1c --- /dev/null +++ b/codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs @@ -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 { + /* 爬楼梯:动态规划 */ + public 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]; + } + + /* 爬楼梯:状态压缩后的动态规划 */ + public 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} 种方案"); + } +} diff --git a/codes/csharp/chapter_dynamic_programming/min_cost_climbing_stairs_dp.cs b/codes/csharp/chapter_dynamic_programming/min_cost_climbing_stairs_dp.cs new file mode 100644 index 000000000..d45cc4bc8 --- /dev/null +++ b/codes/csharp/chapter_dynamic_programming/min_cost_climbing_stairs_dp.cs @@ -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 { + /* 爬楼梯最小代价:动态规划 */ + public 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]; + } + + /* 爬楼梯最小代价:状态压缩后的动态规划 */ + public 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}"); + } +} diff --git a/codes/csharp/utils/PrintUtil.cs b/codes/csharp/utils/PrintUtil.cs index c40d3ec3a..dd9b67a20 100644 --- a/codes/csharp/utils/PrintUtil.cs +++ b/codes/csharp/utils/PrintUtil.cs @@ -18,7 +18,7 @@ public class Trunk { public class PrintUtil { /* Print a list */ - public static void PrintList(List list) { + public static void PrintList(IList list) { Console.WriteLine("[" + string.Join(", ", list) + "]"); }