hello-algo/en/codes/csharp/chapter_dynamic_programming/climbing_stairs_dp.cs

50 lines
1.3 KiB
C#

/**
* 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 {
/* Climbing stairs: Dynamic programming */
int ClimbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// Initialize dp table, used to store subproblem solutions
int[] dp = new int[n + 1];
// Initial state: preset the smallest subproblem solution
dp[1] = 1;
dp[2] = 2;
// State transition: gradually solve larger subproblems from smaller ones
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
/* Climbing stairs: Space-optimized dynamic programming */
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($"Climb {n} steps, there are {res} solutions in total");
res = ClimbingStairsDPComp(n);
Console.WriteLine($"Climb {n} steps, there are {res} solutions in total");
}
}