hello-algo/codes/zig/chapter_dynamic_programming/climbing_stairs_dp.zig
sjinzh b1f8857212
rust and zig : add codes for chapter_dynamic_programming (#606)
* rust : add codes for chapter_dynamic_programming

* zig : add codes for chapter_dynamic_programming

* rust : add codes for chapter_backtracking

* Update n_queens.rs

---------

Co-authored-by: Yudong Jin <krahets@163.com>
2023-07-15 23:16:02 +08:00

51 lines
1.3 KiB
Zig

// File: climbing_stairs_dp.zig
// Created Time: 2023-07-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
// 爬楼梯:动态规划
fn climbing_stairs_dp(comptime n: usize) i32 {
// 已知 dp[1] 和 dp[2] ,返回之
if (n == 1 or n == 2) {
return @intCast(n);
}
// 初始化 dp 表,用于存储子问题的解
var dp = [_]i32{-1} ** (n + 1);
// 初始状态:预设最小子问题的解
dp[1] = 1;
dp[2] = 2;
// 状态转移:从较小子问题逐步求解较大子问题
for (3..n + 1) |i| {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
// 爬楼梯:状态压缩后的动态规划
fn climbing_stairs_dp_comp(comptime n: usize) i32 {
if (n == 1 or n == 2) {
return @intCast(n);
}
var a: i32 = 1;
var b: i32 = 2;
for (3..n + 1) |_| {
var tmp = b;
b = a + b;
a = tmp;
}
return b;
}
// Driver Code
pub fn main() !void {
comptime var n: usize = 9;
var res = climbing_stairs_dp(n);
std.debug.print("爬 {} 阶楼梯共有 {} 种方案\n", .{ n, res });
res = climbing_stairs_dp_comp(n);
std.debug.print("爬 {} 阶楼梯共有 {} 种方案\n", .{ n, res });
_ = try std.io.getStdIn().reader().readByte();
}