diff --git a/codes/ruby/chapter_dynamic_programming/climbing_stairs_backtrack.rb b/codes/ruby/chapter_dynamic_programming/climbing_stairs_backtrack.rb new file mode 100644 index 0000000000..ad0e02bef7 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/climbing_stairs_backtrack.rb @@ -0,0 +1,37 @@ +=begin +File: climbing_stairs_backtrack.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 回溯 ### +def backtrack(choices, state, n, res) + # 当爬到第 n 阶时,方案数量加 1 + res[0] += 1 if state == n + # 遍历所有选择 + for choice in choices + # 剪枝:不允许越过第 n 阶 + next if state + choice > n + + # 尝试:做出选择,更新状态 + backtrack(choices, state + choice, n, res) + end + # 回退 +end + +### 爬楼梯:回溯 ### +def climbing_stairs_backtrack(n) + choices = [1, 2] # 可选择向上爬 1 阶或 2 阶 + state = 0 # 从第 0 阶开始爬 + res = [0] # 使用 res[0] 记录方案数量 + backtrack(choices, state, n, res) + res.first +end + +### Driver Code ### +if __FILE__ == $0 + n = 9 + + res = climbing_stairs_backtrack(n) + puts "爬 #{n} 阶楼梯共有 #{res} 种方案" +end diff --git a/codes/ruby/chapter_dynamic_programming/climbing_stairs_constraint_dp.rb b/codes/ruby/chapter_dynamic_programming/climbing_stairs_constraint_dp.rb new file mode 100644 index 0000000000..1421d09f52 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/climbing_stairs_constraint_dp.rb @@ -0,0 +1,31 @@ +=begin +File: climbing_stairs_constraint_dp.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 带约束爬楼梯:动态规划 ### +def climbing_stairs_backtrack(n) + return 1 if n == 1 || n == 2 + + # 初始化 dp 表,用于存储子问题的解 + dp = Array.new(n + 1) { Array.new(3, 0) } + # 初始状态:预设最小子问题的解 + dp[1][1], dp[1][2] = 1, 0 + dp[2][1], dp[2][2] = 0, 1 + # 状态转移:从较小子问题逐步求解较大子问题 + for i in 3...(n + 1) + dp[i][1] = dp[i - 1][2] + dp[i][2] = dp[i - 2][1] + dp[i - 2][2] + end + + dp[n][1] + dp[n][2] +end + +### Driver Code ### +if __FILE__ == $0 + n = 9 + + res = climbing_stairs_backtrack(n) + puts "爬 #{n} 阶楼梯共有 #{res} 种方案" +end diff --git a/codes/ruby/chapter_dynamic_programming/climbing_stairs_dfs.rb b/codes/ruby/chapter_dynamic_programming/climbing_stairs_dfs.rb new file mode 100644 index 0000000000..ed9b2b39f9 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/climbing_stairs_dfs.rb @@ -0,0 +1,26 @@ +=begin +File: climbing_stairs_dfs.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 搜索 ### +def dfs(i) + # 已知 dp[1] 和 dp[2] ,返回之 + return i if i == 1 || i == 2 + # dp[i] = dp[i-1] + dp[i-2] + dfs(i - 1) + dfs(i - 2) +end + +### 爬楼梯:搜索 ### +def climbing_stairs_dfs(n) + dfs(n) +end + +### Driver Code ### +if __FILE__ == $0 + n = 9 + + res = climbing_stairs_dfs(n) + puts "爬 #{n} 阶楼梯共有 #{res} 种方案" +end diff --git a/codes/ruby/chapter_dynamic_programming/climbing_stairs_dfs_mem.rb b/codes/ruby/chapter_dynamic_programming/climbing_stairs_dfs_mem.rb new file mode 100644 index 0000000000..502e9563e8 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/climbing_stairs_dfs_mem.rb @@ -0,0 +1,33 @@ +=begin +File: climbing_stairs_dfs_mem.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 记忆化搜索 ### +def dfs(i, mem) + # 已知 dp[1] 和 dp[2] ,返回之 + return i if i == 1 || i == 2 + # 若存在记录 dp[i] ,则直接返回之 + return mem[i] if mem[i] != -1 + + # dp[i] = dp[i-1] + dp[i-2] + count = dfs(i - 1, mem) + dfs(i - 2, mem) + # 记录 dp[i] + mem[i] = count +end + +### 爬楼梯:记忆化搜索 ### +def climbing_stairs_dfs_mem(n) + # mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录 + mem = Array.new(n + 1, -1) + dfs(n, mem) +end + +### Driver Code ### +if __FILE__ == $0 + n = 9 + + res = climbing_stairs_dfs_mem(n) + puts "爬 #{n} 阶楼梯共有 #{res} 种方案" +end diff --git a/codes/ruby/chapter_dynamic_programming/climbing_stairs_dp.rb b/codes/ruby/chapter_dynamic_programming/climbing_stairs_dp.rb new file mode 100644 index 0000000000..a88abb4d14 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/climbing_stairs_dp.rb @@ -0,0 +1,40 @@ +=begin +File: climbing_stairs_dp.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 爬楼梯:动态规划 ### +def climbing_stairs_dp(n) + return n if n == 1 || n == 2 + + # 初始化 dp 表,用于存储子问题的解 + dp = Array.new(n + 1, 0) + # 初始状态:预设最小子问题的解 + dp[1], dp[2] = 1, 2 + # 状态转移:从较小子问题逐步求解较大子问题 + (3...(n + 1)).each { |i| dp[i] = dp[i - 1] + dp[i - 2] } + + dp[n] +end + +### 爬楼梯:空间优化后的动态规划 ### +def climbing_stairs_dp_comp(n) + return n if n == 1 || n == 2 + + a, b = 1, 2 + (3...(n + 1)).each { a, b = b, a + b } + + b +end + +### Driver Code ### +if __FILE__ == $0 + n = 9 + + res = climbing_stairs_dp(n) + puts "爬 #{n} 阶楼梯共有 #{res} 种方案" + + res = climbing_stairs_dp_comp(n) + puts "爬 #{n} 阶楼梯共有 #{res} 种方案" +end diff --git a/codes/ruby/chapter_dynamic_programming/coin_change.rb b/codes/ruby/chapter_dynamic_programming/coin_change.rb new file mode 100644 index 0000000000..5135c0f5cb --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/coin_change.rb @@ -0,0 +1,65 @@ +=begin +File: coin_change.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 零钱兑换:动态规划 ### +def coin_change_dp(coins, amt) + n = coins.length + _MAX = amt + 1 + # 初始化 dp 表 + dp = Array.new(n + 1) { Array.new(amt + 1, 0) } + # 状态转移:首行首列 + (1...(amt + 1)).each { |a| dp[0][a] = _MAX } + # 状态转移:其余行和列 + for i in 1...(n + 1) + for a in 1...(amt + 1) + if coins[i - 1] > a + # 若超过目标金额,则不选硬币 i + dp[i][a] = dp[i - 1][a] + else + # 不选和选硬币 i 这两种方案的较小值 + dp[i][a] = [dp[i - 1][a], dp[i][a - coins[i - 1]] + 1].min + end + end + end + dp[n][amt] != _MAX ? dp[n][amt] : -1 +end + +### 零钱兑换:空间优化后的动态规划 ### +def coin_change_dp_comp(coins, amt) + n = coins.length + _MAX = amt + 1 + # 初始化 dp 表 + dp = Array.new(amt + 1, _MAX) + dp[0] = 0 + # 状态转移 + for i in 1...(n + 1) + # 正序遍历 + for a in 1...(amt + 1) + if coins[i - 1] > a + # 若超过目标金额,则不选硬币 i + dp[a] = dp[a] + else + # 不选和选硬币 i 这两种方案的较小值 + dp[a] = [dp[a], dp[a - coins[i - 1]] + 1].min + end + end + end + dp[amt] != _MAX ? dp[amt] : -1 +end + +### Driver Code ### +if __FILE__ == $0 + coins = [1, 2, 5] + amt = 4 + + # 动态规划 + res = coin_change_dp(coins, amt) + puts "凑到目标金额所需的最少硬币数量为 #{res}" + + # 空间优化后的动态规划 + res = coin_change_dp_comp(coins, amt) + puts "凑到目标金额所需的最少硬币数量为 #{res}" +end diff --git a/codes/ruby/chapter_dynamic_programming/coin_change_ii.rb b/codes/ruby/chapter_dynamic_programming/coin_change_ii.rb new file mode 100644 index 0000000000..18cd0475b8 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/coin_change_ii.rb @@ -0,0 +1,63 @@ +=begin +File: coin_change_ii.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 零钱兑换 II:动态规划 ### +def coin_change_ii_dp(coins, amt) + n = coins.length + # 初始化 dp 表 + dp = Array.new(n + 1) { Array.new(amt + 1, 0) } + # 初始化首列 + (0...(n + 1)).each { |i| dp[i][0] = 1 } + # 状态转移 + for i in 1...(n + 1) + for a in 1...(amt + 1) + if coins[i - 1] > a + # 若超过目标金额,则不选硬币 i + dp[i][a] = dp[i - 1][a] + else + # 不选和选硬币 i 这两种方案之和 + dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]] + end + end + end + dp[n][amt] +end + +### 零钱兑换 II:空间优化后的动态规划 ### +def coin_change_ii_dp_comp(coins, amt) + n = coins.length + # 初始化 dp 表 + dp = Array.new(amt + 1, 0) + dp[0] = 1 + # 状态转移 + for i in 1...(n + 1) + # 正序遍历 + for a in 1...(amt + 1) + if coins[i - 1] > a + # 若超过目标金额,则不选硬币 i + dp[a] = dp[a] + else + # 不选和选硬币 i 这两种方案之和 + dp[a] = dp[a] + dp[a - coins[i - 1]] + end + end + end + dp[amt] +end + +### Driver Code ### +if __FILE__ == $0 + coins = [1, 2, 5] + amt = 5 + + # 动态规划 + res = coin_change_ii_dp(coins, amt) + puts "凑出目标金额的硬币组合数量为 #{res}" + + # 空间优化后的动态规划 + res = coin_change_ii_dp_comp(coins, amt) + puts "凑出目标金额的硬币组合数量为 #{res}" +end diff --git a/codes/ruby/chapter_dynamic_programming/edit_distance.rb b/codes/ruby/chapter_dynamic_programming/edit_distance.rb new file mode 100644 index 0000000000..67c6da9168 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/edit_distance.rb @@ -0,0 +1,115 @@ +=begin +File: edit_distance.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 编辑距离:暴力搜索 ### +def edit_distance_dfs(s, t, i, j) + # 若 s 和 t 都为空,则返回 0 + return 0 if i == 0 && j == 0 + # 若 s 为空,则返回 t 长度 + return j if i == 0 + # 若 t 为空,则返回 s 长度 + return i if j == 0 + # 若两字符相等,则直接跳过此两字符 + return edit_distance_dfs(s, t, i - 1, j - 1) if s[i - 1] == t[j - 1] + # 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1 + insert = edit_distance_dfs(s, t, i, j - 1) + delete = edit_distance_dfs(s, t, i - 1, j) + replace = edit_distance_dfs(s, t, i - 1, j - 1) + # 返回最少编辑步数 + [insert, delete, replace].min + 1 +end + +def edit_distance_dfs_mem(s, t, mem, i, j) + # 若 s 和 t 都为空,则返回 0 + return 0 if i == 0 && j == 0 + # 若 s 为空,则返回 t 长度 + return j if i == 0 + # 若 t 为空,则返回 s 长度 + return i if j == 0 + # 若已有记录,则直接返回之 + return mem[i][j] if mem[i][j] != -1 + # 若两字符相等,则直接跳过此两字符 + return edit_distance_dfs_mem(s, t, mem, i - 1, j - 1) if s[i - 1] == t[j - 1] + # 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1 + insert = edit_distance_dfs_mem(s, t, mem, i, j - 1) + delete = edit_distance_dfs_mem(s, t, mem, i - 1, j) + replace = edit_distance_dfs_mem(s, t, mem, i - 1, j - 1) + # 记录并返回最少编辑步数 + mem[i][j] = [insert, delete, replace].min + 1 +end + +### 编辑距离:动态规划 ### +def edit_distance_dp(s, t) + n, m = s.length, t.length + dp = Array.new(n + 1) { Array.new(m + 1, 0) } + # 状态转移:首行首列 + (1...(n + 1)).each { |i| dp[i][0] = i } + (1...(m + 1)).each { |j| dp[0][j] = j } + # 状态转移:其余行和列 + for i in 1...(n + 1) + for j in 1...(m +1) + if s[i - 1] == t[j - 1] + # 若两字符相等,则直接跳过此两字符 + dp[i][j] = dp[i - 1][j - 1] + else + # 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1 + dp[i][j] = [dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]].min + 1 + end + end + end + dp[n][m] +end + +### 编辑距离:空间优化后的动态规划 ### +def edit_distance_dp_comp(s, t) + n, m = s.length, t.length + dp = Array.new(m + 1, 0) + # 状态转移:首行 + (1...(m + 1)).each { |j| dp[j] = j } + # 状态转移:其余行 + for i in 1...(n + 1) + # 状态转移:首列 + leftup = dp.first # 暂存 dp[i-1, j-1] + dp[0] += 1 + # 状态转移:其余列 + for j in 1...(m + 1) + temp = dp[j] + if s[i - 1] == t[j - 1] + # 若两字符相等,则直接跳过此两字符 + dp[j] = leftup + else + # 最少编辑步数 = 插入、删除、替换这三种操作的最少编辑步数 + 1 + dp[j] = [dp[j - 1], dp[j], leftup].min + 1 + end + leftup = temp # 更新为下一轮的 dp[i-1, j-1] + end + end + dp[m] +end + +### Driver Code ### +if __FILE__ == $0 + s = 'bag' + t = 'pack' + n, m = s.length, t.length + + # 暴力搜索 + res = edit_distance_dfs(s, t, n, m) + puts "将 #{s} 更改为 #{t} 最少需要编辑 #{res} 步" + + # 记忆化搜索 + mem = Array.new(n + 1) { Array.new(m + 1, -1) } + res = edit_distance_dfs_mem(s, t, mem, n, m) + puts "将 #{s} 更改为 #{t} 最少需要编辑 #{res} 步" + + # 动态规划 + res = edit_distance_dp(s, t) + puts "将 #{s} 更改为 #{t} 最少需要编辑 #{res} 步" + + # 空间优化后的动态规划 + res = edit_distance_dp_comp(s, t) + puts "将 #{s} 更改为 #{t} 最少需要编辑 #{res} 步" +end diff --git a/codes/ruby/chapter_dynamic_programming/knapsack.rb b/codes/ruby/chapter_dynamic_programming/knapsack.rb new file mode 100644 index 0000000000..c776e7d590 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/knapsack.rb @@ -0,0 +1,99 @@ +=begin +File: knapsack.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 0-1 背包:暴力搜索 ### +def knapsack_dfs(wgt, val, i, c) + # 若已选完所有物品或背包无剩余容量,则返回价值 0 + return 0 if i == 0 || c == 0 + # 若超过背包容量,则只能选择不放入背包 + return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c + # 计算不放入和放入物品 i 的最大价值 + no = knapsack_dfs(wgt, val, i - 1, c) + yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1] + # 返回两种方案中价值更大的那一个 + [no, yes].max +end + +### 0-1 背包:记忆化搜索 ### +def knapsack_dfs_mem(wgt, val, mem, i, c) + # 若已选完所有物品或背包无剩余容量,则返回价值 0 + return 0 if i == 0 || c == 0 + # 若已有记录,则直接返回 + return mem[i][c] if mem[i][c] != -1 + # 若超过背包容量,则只能选择不放入背包 + return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c + # 计算不放入和放入物品 i 的最大价值 + no = knapsack_dfs_mem(wgt, val, mem, i - 1, c) + yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1] + # 记录并返回两种方案中价值更大的那一个 + mem[i][c] = [no, yes].max +end + +### 0-1 背包:动态规划 ### +def knapsack_dp(wgt, val, cap) + n = wgt.length + # 初始化 dp 表 + dp = Array.new(n + 1) { Array.new(cap + 1, 0) } + # 状态转移 + for i in 1...(n + 1) + for c in 1...(cap + 1) + if wgt[i - 1] > c + # 若超过背包容量,则不选物品 i + dp[i][c] = dp[i - 1][c] + else + # 不选和选物品 i 这两种方案的较大值 + dp[i][c] = [dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]].max + end + end + end + dp[n][cap] +end + +### 0-1 背包:空间优化后的动态规划 ### +def knapsack_dp_comp(wgt, val, cap) + n = wgt.length + # 初始化 dp 表 + dp = Array.new(cap + 1, 0) + # 状态转移 + for i in 1...(n + 1) + # 倒序遍历 + for c in cap.downto(1) + if wgt[i - 1] > c + # 若超过背包容量,则不选物品 i + dp[c] = dp[c] + else + # 不选和选物品 i 这两种方案的较大值 + dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max + end + end + end + dp[cap] +end + +### Driver Code ### +if __FILE__ == $0 + wgt = [10, 20, 30, 40, 50] + val = [50, 120, 150, 210, 240] + cap = 50 + n = wgt.length + + # 暴力搜索 + res = knapsack_dfs(wgt, val, n, cap) + puts "不超过背包容量的最大物品价值为 #{res}" + + # 记忆化搜索 + mem = Array.new(n + 1) { Array.new(cap + 1, -1) } + res = knapsack_dfs_mem(wgt, val, mem, n, cap) + puts "不超过背包容量的最大物品价值为 #{res}" + + # 动态规划 + res = knapsack_dp(wgt, val, cap) + puts "不超过背包容量的最大物品价值为 #{res}" + + # 空间优化后的动态规划 + res = knapsack_dp_comp(wgt, val, cap) + puts "不超过背包容量的最大物品价值为 #{res}" +end diff --git a/codes/ruby/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rb b/codes/ruby/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rb new file mode 100644 index 0000000000..f570d2e7b5 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/min_cost_climbing_stairs_dp.rb @@ -0,0 +1,39 @@ +=begin +File: min_cost_climbing_stairs_dp.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 爬楼梯最小代价:动态规划 ### +def min_cost_climbing_stairs_dp(cost) + n = cost.length - 1 + return cost[n] if n == 1 || n == 2 + # 初始化 dp 表,用于存储子问题的解 + dp = Array.new(n + 1, 0) + # 初始状态:预设最小子问题的解 + dp[1], dp[2] = cost[1], cost[2] + # 状态转移:从较小子问题逐步求解较大子问题 + (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] } + dp[n] +end + +# 爬楼梯最小代价:空间优化后的动态规划 +def min_cost_climbing_stairs_dp_comp(cost) + n = cost.length - 1 + return cost[n] if n == 1 || n == 2 + a, b = cost[1], cost[2] + (3...(n + 1)).each { |i| a, b = b, [a, b].min + cost[i] } + b +end + +### Driver Code ### +if __FILE__ == $0 + cost = [0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1] + puts "输入楼梯的代价列表为 #{cost}" + + res = min_cost_climbing_stairs_dp(cost) + puts "爬完楼梯的最低代价为 #{res}" + + res = min_cost_climbing_stairs_dp_comp(cost) + puts "爬完楼梯的最低代价为 #{res}" +end diff --git a/codes/ruby/chapter_dynamic_programming/min_path_sum.rb b/codes/ruby/chapter_dynamic_programming/min_path_sum.rb new file mode 100644 index 0000000000..a18fc7b168 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/min_path_sum.rb @@ -0,0 +1,93 @@ +=begin +File: min_path_sum.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 最小路径和:暴力搜索 ### +def min_path_sum_dfs(grid, i, j) + # 若为左上角单元格,则终止搜索 + return grid[i][j] if i == 0 && j == 0 + # 若行列索引越界,则返回 +∞ 代价 + return Float::INFINITY if i < 0 || j < 0 + # 计算从左上角到 (i-1, j) 和 (i, j-1) 的最小路径代价 + up = min_path_sum_dfs(grid, i - 1, j) + left = min_path_sum_dfs(grid, i, j - 1) + # 返回从左上角到 (i, j) 的最小路径代价 + [left, up].min + grid[i][j] +end + +### 最小路径和:记忆化搜索 ### +def min_path_sum_dfs_mem(grid, mem, i, j) + # 若为左上角单元格,则终止搜索 + return grid[0][0] if i == 0 && j == 0 + # 若行列索引越界,则返回 +∞ 代价 + return Float::INFINITY if i < 0 || j < 0 + # 若已有记录,则直接返回 + return mem[i][j] if mem[i][j] != -1 + # 左边和上边单元格的最小路径代价 + up = min_path_sum_dfs_mem(grid, mem, i - 1, j) + left = min_path_sum_dfs_mem(grid, mem, i, j - 1) + # 记录并返回左上角到 (i, j) 的最小路径代价 + mem[i][j] = [left, up].min + grid[i][j] +end + +### 最小路径和:动态规划 ### +def min_path_sum_dp(grid) + n, m = grid.length, grid.first.length + # 初始化 dp 表 + dp = Array.new(n) { Array.new(m, 0) } + dp[0][0] = grid[0][0] + # 状态转移:首行 + (1...m).each { |j| dp[0][j] = dp[0][j - 1] + grid[0][j] } + # 状态转移:首列 + (1...n).each { |i| dp[i][0] = dp[i - 1][0] + grid[i][0] } + # 状态转移:其余行和列 + for i in 1...n + for j in 1...m + dp[i][j] = [dp[i][j - 1], dp[i - 1][j]].min + grid[i][j] + end + end + dp[n -1][m -1] +end + +### 最小路径和:空间优化后的动态规划 ### +def min_path_sum_dp_comp(grid) + n, m = grid.length, grid.first.length + # 初始化 dp 表 + dp = Array.new(m, 0) + # 状态转移:首行 + dp[0] = grid[0][0] + (1...m).each { |j| dp[j] = dp[j - 1] + grid[0][j] } + # 状态转移:其余行 + for i in 1...n + # 状态转移:首列 + dp[0] = dp[0] + grid[i][0] + # 状态转移:其余列 + (1...m).each { |j| dp[j] = [dp[j - 1], dp[j]].min + grid[i][j] } + end + dp[m - 1] +end + +### Driver Code ### +if __FILE__ == $0 + grid = [[1, 3, 1, 5], [2, 2, 4, 2], [5, 3, 2, 1], [4, 3, 5, 2]] + n, m = grid.length, grid.first.length + + # 暴力搜索 + res = min_path_sum_dfs(grid, n - 1, m - 1) + puts "从左上角到右下角的做小路径和为 #{res}" + + # 记忆化搜索 + mem = Array.new(n) { Array.new(m, - 1) } + res = min_path_sum_dfs_mem(grid, mem, n - 1, m -1) + puts "从左上角到右下角的做小路径和为 #{res}" + + # 动态规划 + res = min_path_sum_dp(grid) + puts "从左上角到右下角的做小路径和为 #{res}" + + # 空间优化后的动态规划 + res = min_path_sum_dp_comp(grid) + puts "从左上角到右下角的做小路径和为 #{res}" +end diff --git a/codes/ruby/chapter_dynamic_programming/unbounded_knapsack.rb b/codes/ruby/chapter_dynamic_programming/unbounded_knapsack.rb new file mode 100644 index 0000000000..f3d5534f08 --- /dev/null +++ b/codes/ruby/chapter_dynamic_programming/unbounded_knapsack.rb @@ -0,0 +1,61 @@ +=begin +File: unbounded_knapsack.rb +Created Time: 2024-05-29 +Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) +=end + +### 完全背包:动态规划 ### +def unbounded_knapsack_dp(wgt, val, cap) + n = wgt.length + # 初始化 dp 表 + dp = Array.new(n + 1) { Array.new(cap + 1, 0) } + # 状态转移 + for i in 1...(n + 1) + for c in 1...(cap + 1) + if wgt[i - 1] > c + # 若超过背包容量,则不选物品 i + dp[i][c] = dp[i - 1][c] + else + # 不选和选物品 i 这两种方案的较大值 + dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max + end + end + end + dp[n][cap] +end + +### 完全背包:空间优化后的动态规划 ##3 +def unbounded_knapsack_dp_comp(wgt, val, cap) + n = wgt.length + # 初始化 dp 表 + dp = Array.new(cap + 1, 0) + # 状态转移 + for i in 1...(n + 1) + # 正序遍历 + for c in 1...(cap + 1) + if wgt[i -1] > c + # 若超过背包容量,则不选物品 i + dp[c] = dp[c] + else + # 不选和选物品 i 这两种方案的较大值 + dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max + end + end + end + dp[cap] +end + +### Driver Code ### +if __FILE__ == $0 + wgt = [1, 2, 3] + val = [5, 11, 15] + cap = 4 + + # 动态规划 + res = unbounded_knapsack_dp(wgt, val, cap) + puts "不超过背包容量的最大物品价值为 #{res}" + + # 空间优化后的动态规划 + res = unbounded_knapsack_dp_comp(wgt, val, cap) + puts "不超过背包容量的最大物品价值为 #{res}" +end