mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-21 02:56:11 +00:00
Translate all code to English (#1836)
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
=begin
|
||||
File: climbing_stairs_backtrack.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Backtracking ###
|
||||
def backtrack(choices, state, n, res)
|
||||
# When climbing to the n-th stair, add 1 to the solution count
|
||||
res[0] += 1 if state == n
|
||||
# Traverse all choices
|
||||
for choice in choices
|
||||
# Pruning: not allowed to go beyond the n-th stair
|
||||
next if state + choice > n
|
||||
|
||||
# Attempt: make choice, update state
|
||||
backtrack(choices, state + choice, n, res)
|
||||
end
|
||||
# Backtrack
|
||||
end
|
||||
|
||||
### Climbing stairs: backtracking ###
|
||||
def climbing_stairs_backtrack(n)
|
||||
choices = [1, 2] # Can choose to climb up 1 or 2 stairs
|
||||
state = 0 # Start climbing from the 0-th stair
|
||||
res = [0] # Use res[0] to record the solution count
|
||||
backtrack(choices, state, n, res)
|
||||
res.first
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 9
|
||||
|
||||
res = climbing_stairs_backtrack(n)
|
||||
puts "Climbing #{n} stairs has #{res} solutions"
|
||||
end
|
||||
@@ -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
|
||||
|
||||
### Climbing stairs with constraint: DP ###
|
||||
def climbing_stairs_constraint_dp(n)
|
||||
return 1 if n == 1 || n == 2
|
||||
|
||||
# Initialize dp table, used to store solutions to subproblems
|
||||
dp = Array.new(n + 1) { Array.new(3, 0) }
|
||||
# Initial state: preset the solution to the smallest subproblem
|
||||
dp[1][1], dp[1][2] = 1, 0
|
||||
dp[2][1], dp[2][2] = 0, 1
|
||||
# State transition: gradually solve larger subproblems from smaller ones
|
||||
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_constraint_dp(n)
|
||||
puts "Climbing #{n} stairs has #{res} solutions"
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
=begin
|
||||
File: climbing_stairs_dfs.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Search ###
|
||||
def dfs(i)
|
||||
# Known dp[1] and dp[2], return them
|
||||
return i if i == 1 || i == 2
|
||||
# dp[i] = dp[i-1] + dp[i-2]
|
||||
dfs(i - 1) + dfs(i - 2)
|
||||
end
|
||||
|
||||
### Climbing stairs: search ###
|
||||
def climbing_stairs_dfs(n)
|
||||
dfs(n)
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 9
|
||||
|
||||
res = climbing_stairs_dfs(n)
|
||||
puts "Climbing #{n} stairs has #{res} solutions"
|
||||
end
|
||||
@@ -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
|
||||
|
||||
### Memoization search ###
|
||||
def dfs(i, mem)
|
||||
# Known dp[1] and dp[2], return them
|
||||
return i if i == 1 || i == 2
|
||||
# If record dp[i] exists, return it directly
|
||||
return mem[i] if mem[i] != -1
|
||||
|
||||
# dp[i] = dp[i-1] + dp[i-2]
|
||||
count = dfs(i - 1, mem) + dfs(i - 2, mem)
|
||||
# Record dp[i]
|
||||
mem[i] = count
|
||||
end
|
||||
|
||||
### Climbing stairs: memoization search ###
|
||||
def climbing_stairs_dfs_mem(n)
|
||||
# mem[i] records the total number of solutions to climb to the i-th stair, -1 means no record
|
||||
mem = Array.new(n + 1, -1)
|
||||
dfs(n, mem)
|
||||
end
|
||||
|
||||
### Driver Code ###
|
||||
if __FILE__ == $0
|
||||
n = 9
|
||||
|
||||
res = climbing_stairs_dfs_mem(n)
|
||||
puts "Climbing #{n} stairs has #{res} solutions"
|
||||
end
|
||||
@@ -0,0 +1,40 @@
|
||||
=begin
|
||||
File: climbing_stairs_dp.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Climbing stairs: dynamic programming ###
|
||||
def climbing_stairs_dp(n)
|
||||
return n if n == 1 || n == 2
|
||||
|
||||
# Initialize dp table, used to store solutions to subproblems
|
||||
dp = Array.new(n + 1, 0)
|
||||
# Initial state: preset the solution to the smallest subproblem
|
||||
dp[1], dp[2] = 1, 2
|
||||
# State transition: gradually solve larger subproblems from smaller ones
|
||||
(3...(n + 1)).each { |i| dp[i] = dp[i - 1] + dp[i - 2] }
|
||||
|
||||
dp[n]
|
||||
end
|
||||
|
||||
### Climbing stairs: space-optimized DP ###
|
||||
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 "Climbing #{n} stairs has #{res} solutions"
|
||||
|
||||
res = climbing_stairs_dp_comp(n)
|
||||
puts "Climbing #{n} stairs has #{res} solutions"
|
||||
end
|
||||
@@ -0,0 +1,65 @@
|
||||
=begin
|
||||
File: coin_change.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Coin change: dynamic programming ###
|
||||
def coin_change_dp(coins, amt)
|
||||
n = coins.length
|
||||
_MAX = amt + 1
|
||||
# Initialize dp table
|
||||
dp = Array.new(n + 1) { Array.new(amt + 1, 0) }
|
||||
# State transition: first row and first column
|
||||
(1...(amt + 1)).each { |a| dp[0][a] = _MAX }
|
||||
# State transition: rest of the rows and columns
|
||||
for i in 1...(n + 1)
|
||||
for a in 1...(amt + 1)
|
||||
if coins[i - 1] > a
|
||||
# If exceeds target amount, don't select coin i
|
||||
dp[i][a] = dp[i - 1][a]
|
||||
else
|
||||
# The smaller value between not selecting and selecting coin 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
|
||||
|
||||
### Coin change: space-optimized DP ###
|
||||
def coin_change_dp_comp(coins, amt)
|
||||
n = coins.length
|
||||
_MAX = amt + 1
|
||||
# Initialize dp table
|
||||
dp = Array.new(amt + 1, _MAX)
|
||||
dp[0] = 0
|
||||
# State transition
|
||||
for i in 1...(n + 1)
|
||||
# Traverse in forward order
|
||||
for a in 1...(amt + 1)
|
||||
if coins[i - 1] > a
|
||||
# If exceeds target amount, don't select coin i
|
||||
dp[a] = dp[a]
|
||||
else
|
||||
# The smaller value between not selecting and selecting coin 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
|
||||
|
||||
# Dynamic programming
|
||||
res = coin_change_dp(coins, amt)
|
||||
puts "Minimum coins needed to make target amount is #{res}"
|
||||
|
||||
# Space-optimized dynamic programming
|
||||
res = coin_change_dp_comp(coins, amt)
|
||||
puts "Minimum coins needed to make target amount is #{res}"
|
||||
end
|
||||
@@ -0,0 +1,63 @@
|
||||
=begin
|
||||
File: coin_change_ii.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Coin change II: dynamic programming ###
|
||||
def coin_change_ii_dp(coins, amt)
|
||||
n = coins.length
|
||||
# Initialize dp table
|
||||
dp = Array.new(n + 1) { Array.new(amt + 1, 0) }
|
||||
# Initialize first column
|
||||
(0...(n + 1)).each { |i| dp[i][0] = 1 }
|
||||
# State transition
|
||||
for i in 1...(n + 1)
|
||||
for a in 1...(amt + 1)
|
||||
if coins[i - 1] > a
|
||||
# If exceeds target amount, don't select coin i
|
||||
dp[i][a] = dp[i - 1][a]
|
||||
else
|
||||
# Sum of the two options: not selecting and selecting coin i
|
||||
dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]
|
||||
end
|
||||
end
|
||||
end
|
||||
dp[n][amt]
|
||||
end
|
||||
|
||||
### Coin change II: space-optimized DP ###
|
||||
def coin_change_ii_dp_comp(coins, amt)
|
||||
n = coins.length
|
||||
# Initialize dp table
|
||||
dp = Array.new(amt + 1, 0)
|
||||
dp[0] = 1
|
||||
# State transition
|
||||
for i in 1...(n + 1)
|
||||
# Traverse in forward order
|
||||
for a in 1...(amt + 1)
|
||||
if coins[i - 1] > a
|
||||
# If exceeds target amount, don't select coin i
|
||||
dp[a] = dp[a]
|
||||
else
|
||||
# Sum of the two options: not selecting and selecting coin 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
|
||||
|
||||
# Dynamic programming
|
||||
res = coin_change_ii_dp(coins, amt)
|
||||
puts "Number of coin combinations to make target amount is #{res}"
|
||||
|
||||
# Space-optimized dynamic programming
|
||||
res = coin_change_ii_dp_comp(coins, amt)
|
||||
puts "Number of coin combinations to make target amount is #{res}"
|
||||
end
|
||||
@@ -0,0 +1,115 @@
|
||||
=begin
|
||||
File: edit_distance.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Edit distance: brute force search ###
|
||||
def edit_distance_dfs(s, t, i, j)
|
||||
# If both s and t are empty, return 0
|
||||
return 0 if i == 0 && j == 0
|
||||
# If s is empty, return length of t
|
||||
return j if i == 0
|
||||
# If t is empty, return length of s
|
||||
return i if j == 0
|
||||
# If two characters are equal, skip both characters
|
||||
return edit_distance_dfs(s, t, i - 1, j - 1) if s[i - 1] == t[j - 1]
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 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)
|
||||
# Return minimum edit steps
|
||||
[insert, delete, replace].min + 1
|
||||
end
|
||||
|
||||
def edit_distance_dfs_mem(s, t, mem, i, j)
|
||||
# If both s and t are empty, return 0
|
||||
return 0 if i == 0 && j == 0
|
||||
# If s is empty, return length of t
|
||||
return j if i == 0
|
||||
# If t is empty, return length of s
|
||||
return i if j == 0
|
||||
# If there's a record, return it directly
|
||||
return mem[i][j] if mem[i][j] != -1
|
||||
# If two characters are equal, skip both characters
|
||||
return edit_distance_dfs_mem(s, t, mem, i - 1, j - 1) if s[i - 1] == t[j - 1]
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 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)
|
||||
# Record and return minimum edit steps
|
||||
mem[i][j] = [insert, delete, replace].min + 1
|
||||
end
|
||||
|
||||
### Edit distance: dynamic programming ###
|
||||
def edit_distance_dp(s, t)
|
||||
n, m = s.length, t.length
|
||||
dp = Array.new(n + 1) { Array.new(m + 1, 0) }
|
||||
# State transition: first row and first column
|
||||
(1...(n + 1)).each { |i| dp[i][0] = i }
|
||||
(1...(m + 1)).each { |j| dp[0][j] = j }
|
||||
# State transition: rest of the rows and columns
|
||||
for i in 1...(n + 1)
|
||||
for j in 1...(m +1)
|
||||
if s[i - 1] == t[j - 1]
|
||||
# If two characters are equal, skip both characters
|
||||
dp[i][j] = dp[i - 1][j - 1]
|
||||
else
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 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
|
||||
|
||||
### Edit distance: space-optimized DP ###
|
||||
def edit_distance_dp_comp(s, t)
|
||||
n, m = s.length, t.length
|
||||
dp = Array.new(m + 1, 0)
|
||||
# State transition: first row
|
||||
(1...(m + 1)).each { |j| dp[j] = j }
|
||||
# State transition: rest of the rows
|
||||
for i in 1...(n + 1)
|
||||
# State transition: first column
|
||||
leftup = dp.first # Temporarily store dp[i-1, j-1]
|
||||
dp[0] += 1
|
||||
# State transition: rest of the columns
|
||||
for j in 1...(m + 1)
|
||||
temp = dp[j]
|
||||
if s[i - 1] == t[j - 1]
|
||||
# If two characters are equal, skip both characters
|
||||
dp[j] = leftup
|
||||
else
|
||||
# Minimum edit steps = minimum edit steps of insert, delete, replace + 1
|
||||
dp[j] = [dp[j - 1], dp[j], leftup].min + 1
|
||||
end
|
||||
leftup = temp # Update for next round's 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
|
||||
|
||||
# Brute-force search
|
||||
res = edit_distance_dfs(s, t, n, m)
|
||||
puts "Changing #{s} to #{t} requires minimum #{res} edits"
|
||||
|
||||
# Memoization search
|
||||
mem = Array.new(n + 1) { Array.new(m + 1, -1) }
|
||||
res = edit_distance_dfs_mem(s, t, mem, n, m)
|
||||
puts "Changing #{s} to #{t} requires minimum #{res} edits"
|
||||
|
||||
# Dynamic programming
|
||||
res = edit_distance_dp(s, t)
|
||||
puts "Changing #{s} to #{t} requires minimum #{res} edits"
|
||||
|
||||
# Space-optimized dynamic programming
|
||||
res = edit_distance_dp_comp(s, t)
|
||||
puts "Changing #{s} to #{t} requires minimum #{res} edits"
|
||||
end
|
||||
@@ -0,0 +1,99 @@
|
||||
=begin
|
||||
File: knapsack.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### 0-1 knapsack: brute force search ###
|
||||
def knapsack_dfs(wgt, val, i, c)
|
||||
# If all items have been selected or knapsack has no remaining capacity, return value 0
|
||||
return 0 if i == 0 || c == 0
|
||||
# If exceeds knapsack capacity, can only choose not to put it in
|
||||
return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c
|
||||
# Calculate the maximum value of not putting in and putting in item i
|
||||
no = knapsack_dfs(wgt, val, i - 1, c)
|
||||
yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]
|
||||
# Return the larger value of the two options
|
||||
[no, yes].max
|
||||
end
|
||||
|
||||
### 0-1 knapsack: memoization search ###
|
||||
def knapsack_dfs_mem(wgt, val, mem, i, c)
|
||||
# If all items have been selected or knapsack has no remaining capacity, return value 0
|
||||
return 0 if i == 0 || c == 0
|
||||
# If there's a record, return it directly
|
||||
return mem[i][c] if mem[i][c] != -1
|
||||
# If exceeds knapsack capacity, can only choose not to put it in
|
||||
return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c
|
||||
# Calculate the maximum value of not putting in and putting in item 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]
|
||||
# Record and return the larger value of the two options
|
||||
mem[i][c] = [no, yes].max
|
||||
end
|
||||
|
||||
### 0-1 knapsack: dynamic programming ###
|
||||
def knapsack_dp(wgt, val, cap)
|
||||
n = wgt.length
|
||||
# Initialize dp table
|
||||
dp = Array.new(n + 1) { Array.new(cap + 1, 0) }
|
||||
# State transition
|
||||
for i in 1...(n + 1)
|
||||
for c in 1...(cap + 1)
|
||||
if wgt[i - 1] > c
|
||||
# If exceeds knapsack capacity, don't select item i
|
||||
dp[i][c] = dp[i - 1][c]
|
||||
else
|
||||
# The larger value between not selecting and selecting item 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 knapsack: space-optimized DP ###
|
||||
def knapsack_dp_comp(wgt, val, cap)
|
||||
n = wgt.length
|
||||
# Initialize dp table
|
||||
dp = Array.new(cap + 1, 0)
|
||||
# State transition
|
||||
for i in 1...(n + 1)
|
||||
# Traverse in reverse order
|
||||
for c in cap.downto(1)
|
||||
if wgt[i - 1] > c
|
||||
# If exceeds knapsack capacity, don't select item i
|
||||
dp[c] = dp[c]
|
||||
else
|
||||
# The larger value between not selecting and selecting item 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
|
||||
|
||||
# Brute-force search
|
||||
res = knapsack_dfs(wgt, val, n, cap)
|
||||
puts "Maximum item value not exceeding knapsack capacity is #{res}"
|
||||
|
||||
# Memoization search
|
||||
mem = Array.new(n + 1) { Array.new(cap + 1, -1) }
|
||||
res = knapsack_dfs_mem(wgt, val, mem, n, cap)
|
||||
puts "Maximum item value not exceeding knapsack capacity is #{res}"
|
||||
|
||||
# Dynamic programming
|
||||
res = knapsack_dp(wgt, val, cap)
|
||||
puts "Maximum item value not exceeding knapsack capacity is #{res}"
|
||||
|
||||
# Space-optimized dynamic programming
|
||||
res = knapsack_dp_comp(wgt, val, cap)
|
||||
puts "Maximum item value not exceeding knapsack capacity is #{res}"
|
||||
end
|
||||
@@ -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
|
||||
|
||||
### Minimum cost climbing stairs: DP ###
|
||||
def min_cost_climbing_stairs_dp(cost)
|
||||
n = cost.length - 1
|
||||
return cost[n] if n == 1 || n == 2
|
||||
# Initialize dp table, used to store solutions to subproblems
|
||||
dp = Array.new(n + 1, 0)
|
||||
# Initial state: preset the solution to the smallest subproblem
|
||||
dp[1], dp[2] = cost[1], cost[2]
|
||||
# State transition: gradually solve larger subproblems from smaller ones
|
||||
(3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }
|
||||
dp[n]
|
||||
end
|
||||
|
||||
# Minimum cost climbing stairs: Space-optimized dynamic programming
|
||||
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 "Input stair cost list is #{cost}"
|
||||
|
||||
res = min_cost_climbing_stairs_dp(cost)
|
||||
puts "Minimum cost to climb stairs is #{res}"
|
||||
|
||||
res = min_cost_climbing_stairs_dp_comp(cost)
|
||||
puts "Minimum cost to climb stairs is #{res}"
|
||||
end
|
||||
@@ -0,0 +1,93 @@
|
||||
=begin
|
||||
File: min_path_sum.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Minimum path sum: brute force search ###
|
||||
def min_path_sum_dfs(grid, i, j)
|
||||
# If it's the top-left cell, terminate the search
|
||||
return grid[i][j] if i == 0 && j == 0
|
||||
# If row or column index is out of bounds, return +∞ cost
|
||||
return Float::INFINITY if i < 0 || j < 0
|
||||
# Calculate the minimum path cost from top-left to (i-1, j) and (i, j-1)
|
||||
up = min_path_sum_dfs(grid, i - 1, j)
|
||||
left = min_path_sum_dfs(grid, i, j - 1)
|
||||
# Return the minimum path cost from top-left to (i, j)
|
||||
[left, up].min + grid[i][j]
|
||||
end
|
||||
|
||||
### Minimum path sum: memoization search ###
|
||||
def min_path_sum_dfs_mem(grid, mem, i, j)
|
||||
# If it's the top-left cell, terminate the search
|
||||
return grid[0][0] if i == 0 && j == 0
|
||||
# If row or column index is out of bounds, return +∞ cost
|
||||
return Float::INFINITY if i < 0 || j < 0
|
||||
# If there's a record, return it directly
|
||||
return mem[i][j] if mem[i][j] != -1
|
||||
# Minimum path cost for left and upper cells
|
||||
up = min_path_sum_dfs_mem(grid, mem, i - 1, j)
|
||||
left = min_path_sum_dfs_mem(grid, mem, i, j - 1)
|
||||
# Record and return the minimum path cost from top-left to (i, j)
|
||||
mem[i][j] = [left, up].min + grid[i][j]
|
||||
end
|
||||
|
||||
### Minimum path sum: dynamic programming ###
|
||||
def min_path_sum_dp(grid)
|
||||
n, m = grid.length, grid.first.length
|
||||
# Initialize dp table
|
||||
dp = Array.new(n) { Array.new(m, 0) }
|
||||
dp[0][0] = grid[0][0]
|
||||
# State transition: first row
|
||||
(1...m).each { |j| dp[0][j] = dp[0][j - 1] + grid[0][j] }
|
||||
# State transition: first column
|
||||
(1...n).each { |i| dp[i][0] = dp[i - 1][0] + grid[i][0] }
|
||||
# State transition: rest of the rows and columns
|
||||
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
|
||||
|
||||
### Minimum path sum: space-optimized DP ###
|
||||
def min_path_sum_dp_comp(grid)
|
||||
n, m = grid.length, grid.first.length
|
||||
# Initialize dp table
|
||||
dp = Array.new(m, 0)
|
||||
# State transition: first row
|
||||
dp[0] = grid[0][0]
|
||||
(1...m).each { |j| dp[j] = dp[j - 1] + grid[0][j] }
|
||||
# State transition: rest of the rows
|
||||
for i in 1...n
|
||||
# State transition: first column
|
||||
dp[0] = dp[0] + grid[i][0]
|
||||
# State transition: rest of the columns
|
||||
(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
|
||||
|
||||
# Brute-force search
|
||||
res = min_path_sum_dfs(grid, n - 1, m - 1)
|
||||
puts "Minimum path sum from top-left to bottom-right is #{res}"
|
||||
|
||||
# Memoization search
|
||||
mem = Array.new(n) { Array.new(m, - 1) }
|
||||
res = min_path_sum_dfs_mem(grid, mem, n - 1, m -1)
|
||||
puts "Minimum path sum from top-left to bottom-right is #{res}"
|
||||
|
||||
# Dynamic programming
|
||||
res = min_path_sum_dp(grid)
|
||||
puts "Minimum path sum from top-left to bottom-right is #{res}"
|
||||
|
||||
# Space-optimized dynamic programming
|
||||
res = min_path_sum_dp_comp(grid)
|
||||
puts "Minimum path sum from top-left to bottom-right is #{res}"
|
||||
end
|
||||
@@ -0,0 +1,61 @@
|
||||
=begin
|
||||
File: unbounded_knapsack.rb
|
||||
Created Time: 2024-05-29
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
### Unbounded knapsack: dynamic programming ###
|
||||
def unbounded_knapsack_dp(wgt, val, cap)
|
||||
n = wgt.length
|
||||
# Initialize dp table
|
||||
dp = Array.new(n + 1) { Array.new(cap + 1, 0) }
|
||||
# State transition
|
||||
for i in 1...(n + 1)
|
||||
for c in 1...(cap + 1)
|
||||
if wgt[i - 1] > c
|
||||
# If exceeds knapsack capacity, don't select item i
|
||||
dp[i][c] = dp[i - 1][c]
|
||||
else
|
||||
# The larger value between not selecting and selecting item 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
|
||||
|
||||
### Unbounded knapsack: space-optimized DP ###
|
||||
def unbounded_knapsack_dp_comp(wgt, val, cap)
|
||||
n = wgt.length
|
||||
# Initialize dp table
|
||||
dp = Array.new(cap + 1, 0)
|
||||
# State transition
|
||||
for i in 1...(n + 1)
|
||||
# Traverse in forward order
|
||||
for c in 1...(cap + 1)
|
||||
if wgt[i -1] > c
|
||||
# If exceeds knapsack capacity, don't select item i
|
||||
dp[c] = dp[c]
|
||||
else
|
||||
# The larger value between not selecting and selecting item 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
|
||||
|
||||
# Dynamic programming
|
||||
res = unbounded_knapsack_dp(wgt, val, cap)
|
||||
puts "Maximum item value not exceeding knapsack capacity is #{res}"
|
||||
|
||||
# Space-optimized dynamic programming
|
||||
res = unbounded_knapsack_dp_comp(wgt, val, cap)
|
||||
puts "Maximum item value not exceeding knapsack capacity is #{res}"
|
||||
end
|
||||
Reference in New Issue
Block a user