Files
hello-algo/en/codes/java/chapter_dynamic_programming/climbing_stairs_dp.java
T
Yudong Jin 14c314c08c Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
2026-10-31 03:35:51 +08:00

49 lines
1.4 KiB
Java

/**
* File: climbing_stairs_dp.java
* Created Time: 2023-06-30
* Author: krahets ([email protected])
*/
package chapter_dynamic_programming;
public class climbing_stairs_dp {
/* Climbing stairs: dynamic programming */
public static int climbingStairsDP(int n) {
if (n == 1 || n == 2)
return n;
// Initialize the dp table to store subproblem solutions
int[] dp = new int[n + 1];
// Initial states: set the solutions to the smallest subproblems
dp[1] = 1;
dp[2] = 2;
// State transitions: solve progressively larger subproblems using 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 */
public static 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;
}
public static void main(String[] args) {
int n = 9;
int res = climbingStairsDP(n);
System.out.println(String.format("Number of ways to climb %d stairs: %d", n, res));
res = climbingStairsDPComp(n);
System.out.println(String.format("Number of ways to climb %d stairs: %d", n, res));
}
}