@ -9,7 +9,7 @@
/* 带约束爬楼梯:动态规划 */
int climbingStairsConstraintDP(int n) {
if (n == 1 || n == 2) {
return n;
return 1;
}
// 初始化 dp 表,用于存储子问题的解
vector<vector<int>> dp(n + 1, vector<int>(3, 0));
@ -10,7 +10,7 @@ public class climbing_stairs_constraint_dp {
public int climbingStairsConstraintDP(int n) {
int[,] dp = new int[n + 1, 3];
@ -7,7 +7,7 @@
List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));
@ -7,7 +7,7 @@ package chapter_dynamic_programming
func climbingStairsConstraintDP(n int) int {
if n == 1 || n == 2 {
return n
return 1
dp := make([][3]int, n+1)
static int climbingStairsConstraintDP(int n) {
int[][] dp = new int[n + 1][3];
function climbingStairsConstraintDP(n) {
if (n === 1 || n === 2) {
const dp = Array.from(new Array(n + 1), () => new Array(3));
@ -8,7 +8,7 @@ Author: Krahets (krahets@163.com)
def climbing_stairs_constraint_dp(n: int) -> int:
"""带约束爬楼梯:动态规划"""
if n == 1 or n == 2:
# 初始化 dp 表,用于存储子问题的解
dp = [[0] * 3 for _ in range(n + 1)]
# 初始状态:预设最小子问题的解
@ -6,7 +6,7 @@
fn climbing_stairs_constraint_dp(n: usize) -> i32 {
if n == 1 || n == 2 { return n as i32 };
if n == 1 || n == 2 { return 1 };
let mut dp = vec![vec![-1; 3]; n + 1];
// 初始状态:预设最小子问题的解
func climbingStairsConstraintDP(n: Int) -> Int {
var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)
function climbingStairsConstraintDP(n: number): number {
const dp = Array.from(
@ -7,7 +7,7 @@ const std = @import("std");
// 带约束爬楼梯:动态规划
fn climbingStairsConstraintDP(comptime n: usize) i32 {
if (n == 1 or n == 2) {
return @intCast(n);
var dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);