Sync zh and zh-hant versions

This commit is contained in:
krahets 2024-05-31 12:44:10 +08:00
parent d5b0e2f457
commit e0467b692d
22 changed files with 764 additions and 60 deletions

View File

@ -97,7 +97,9 @@ def linear_log_recur(n: int) -> int:
"""線性對數階"""
if n <= 1:
return 1
count: int = linear_log_recur(n // 2) + linear_log_recur(n // 2)
# 一分為二,子問題的規模減小一半
count = linear_log_recur(n // 2) + linear_log_recur(n // 2)
# 當前子問題包含 n 個操作
for _ in range(n):
count += 1
return count
@ -120,32 +122,32 @@ if __name__ == "__main__":
n = 8
print("輸入資料大小 n =", n)
count: int = constant(n)
count = constant(n)
print("常數階的操作數量 =", count)
count: int = linear(n)
count = linear(n)
print("線性階的操作數量 =", count)
count: int = array_traversal([0] * n)
count = array_traversal([0] * n)
print("線性階(走訪陣列)的操作數量 =", count)
count: int = quadratic(n)
count = quadratic(n)
print("平方階的操作數量 =", count)
nums = [i for i in range(n, 0, -1)] # [n, n-1, ..., 2, 1]
count: int = bubble_sort(nums)
count = bubble_sort(nums)
print("平方階(泡沫排序)的操作數量 =", count)
count: int = exponential(n)
count = exponential(n)
print("指數階(迴圈實現)的操作數量 =", count)
count: int = exp_recur(n)
count = exp_recur(n)
print("指數階(遞迴實現)的操作數量 =", count)
count: int = logarithmic(n)
count = logarithmic(n)
print("對數階(迴圈實現)的操作數量 =", count)
count: int = log_recur(n)
count = log_recur(n)
print("對數階(遞迴實現)的操作數量 =", count)
count: int = linear_log_recur(n)
count = linear_log_recur(n)
print("線性對數階(遞迴實現)的操作數量 =", count)
count: int = factorial_recur(n)
count = factorial_recur(n)
print("階乘階(遞迴實現)的操作數量 =", count)

View File

@ -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

View File

@ -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_constraint_dp(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_constraint_dp(n)
puts "#{n} 階樓梯共有 #{res} 種方案"
end

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -74,7 +74,6 @@ fn find(nums: &[i32], target: i32) -> Option<usize> {
fn main() {
/* 初始化陣列 */
let arr: [i32; 5] = [0; 5];
let slice: &[i32] = &[0; 5];
print!("陣列 arr = ");
print_util::print_array(&arr);
// 在 Rust 中,指定長度時([i32; 5])為陣列,不指定長度時(&[i32])為切片

View File

@ -10,7 +10,7 @@ use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};
/* 前序走訪:例題一 */
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {
if root.is_none() {
return;
}
@ -19,8 +19,8 @@ fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeN
// 記錄解
res.push(node.clone());
}
pre_order(res, node.borrow().left.clone());
pre_order(res, node.borrow().right.clone());
pre_order(res, node.borrow().left.as_ref());
pre_order(res, node.borrow().right.as_ref());
}
}
@ -32,7 +32,7 @@ pub fn main() {
// 前序走訪
let mut res = Vec::new();
pre_order(&mut res, root);
pre_order(&mut res, root.as_ref());
println!("\n輸出所有值為 7 的節點");
let mut vals = Vec::new();

View File

@ -13,7 +13,7 @@ use tree_node::{vec_to_tree, TreeNode};
fn pre_order(
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
path: &mut Vec<Rc<RefCell<TreeNode>>>,
root: Option<Rc<RefCell<TreeNode>>>,
root: Option<&Rc<RefCell<TreeNode>>>,
) {
if root.is_none() {
return;
@ -25,10 +25,10 @@ fn pre_order(
// 記錄解
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
pre_order(res, path, node.borrow().left.as_ref());
pre_order(res, path, node.borrow().right.as_ref());
// 回退
path.remove(path.len() - 1);
path.pop();
}
}
@ -41,7 +41,7 @@ pub fn main() {
// 前序走訪
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);
pre_order(&mut res, &mut path, root.as_ref());
println!("\n輸出所有根節點到節點 7 的路徑");
for path in res {

View File

@ -13,7 +13,7 @@ use tree_node::{vec_to_tree, TreeNode};
fn pre_order(
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
path: &mut Vec<Rc<RefCell<TreeNode>>>,
root: Option<Rc<RefCell<TreeNode>>>,
root: Option<&Rc<RefCell<TreeNode>>>,
) {
// 剪枝
if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {
@ -26,10 +26,10 @@ fn pre_order(
// 記錄解
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
pre_order(res, path, node.borrow().left.as_ref());
pre_order(res, path, node.borrow().right.as_ref());
// 回退
path.remove(path.len() - 1);
path.pop();
}
}
@ -42,7 +42,7 @@ pub fn main() {
// 前序走訪
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);
pre_order(&mut res, &mut path, root.as_ref());
println!("\n輸出所有根節點到節點 7 的路徑,路徑中不包含值為 3 的節點");
for path in res {

View File

@ -11,7 +11,7 @@ use tree_node::{vec_to_tree, TreeNode};
/* 判斷當前狀態是否為解 */
fn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {
return !state.is_empty() && state.get(state.len() - 1).unwrap().borrow().val == 7;
return !state.is_empty() && state.last().unwrap().borrow().val == 7;
}
/* 記錄解 */
@ -23,8 +23,8 @@ fn record_solution(
}
/* 判斷在當前狀態下,該選擇是否合法 */
fn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) -> bool {
return choice.borrow().val != 3;
fn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {
return choice.is_some() && choice.unwrap().borrow().val != 3;
}
/* 更新狀態 */
@ -34,13 +34,13 @@ fn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNo
/* 恢復狀態 */
fn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {
state.remove(state.len() - 1);
state.pop();
}
/* 回溯演算法:例題三 */
fn backtrack(
state: &mut Vec<Rc<RefCell<TreeNode>>>,
choices: &mut Vec<Rc<RefCell<TreeNode>>>,
choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
) {
// 檢查是否為解
@ -49,22 +49,22 @@ fn backtrack(
record_solution(state, res);
}
// 走訪所有選擇
for choice in choices {
for &choice in choices.iter() {
// 剪枝:檢查選擇是否合法
if is_valid(state, choice.clone()) {
if is_valid(state, choice) {
// 嘗試:做出選擇,更新狀態
make_choice(state, choice.clone());
make_choice(state, choice.unwrap().clone());
// 進行下一輪選擇
backtrack(
state,
&mut vec![
choice.borrow().left.clone().unwrap(),
choice.borrow().right.clone().unwrap(),
&vec![
choice.unwrap().borrow().left.as_ref(),
choice.unwrap().borrow().right.as_ref(),
],
res,
);
// 回退:撤銷選擇,恢復到之前的狀態
undo_choice(state, choice.clone());
undo_choice(state, choice.unwrap().clone());
}
}
}
@ -77,7 +77,7 @@ pub fn main() {
// 回溯演算法
let mut res = Vec::new();
backtrack(&mut Vec::new(), &mut vec![root.unwrap()], &mut res);
backtrack(&mut Vec::new(), &mut vec![root.as_ref()], &mut res);
println!("\n輸出所有根節點到節點 7 的路徑,要求路徑中不包含值為 3 的節點");
for path in res {

View File

@ -7,7 +7,7 @@
也就是說,在能夠解決問題的前提下,演算法效率已成為衡量演算法優劣的主要評價指標,它包括以下兩個維度。
- **時間效率**:演算法執行速度的快慢
- **時間效率**:演算法執行時間的長短
- **空間效率**:演算法佔用記憶體空間的大小。
簡而言之,**我們的目標是設計“既快又省”的資料結構與演算法**。而有效地評估演算法效率至關重要,因為只有這樣,我們才能將各種演算法進行對比,進而指導演算法設計與最佳化過程。
@ -18,7 +18,7 @@
假設我們現在有演算法 `A` 和演算法 `B` ,它們都能解決同一問題,現在需要對比這兩個演算法的效率。最直接的方法是找一臺計算機,執行這兩個演算法,並監控記錄它們的執行時間和記憶體佔用情況。這種評估方式能夠反映真實情況,但也存在較大的侷限性。
一方面,**難以排除測試環境的干擾因素**。硬體配置會影響演算法的效能。比如在某臺計算機中,演算法 `A` 的執行時間比演算法 `B` 短;但在另一臺配置不同的計算機中,可能得到相反的測試結果。這意味著我們需要在各種機器上進行測試,統計平均效率,而這是不現實的。
一方面,**難以排除測試環境的干擾因素**。硬體配置會影響演算法的效能表現。比如一個演算法的並行度較高,那麼它就更適合在多核 CPU 上執行,一個演算法的記憶體操作密集,那麼它在高效能記憶體上的表現就會更好。也就是說,演算法在不同的機器上的測試結果可能是不一致的。這意味著我們需要在各種機器上進行測試,統計平均效率,而這是不現實的。
另一方面,**展開完整測試非常耗費資源**。隨著輸入資料量的變化,演算法會表現出不同的效率。例如,在輸入資料量較小時,演算法 `A` 的執行時間比演算法 `B` 短;而在輸入資料量較大時,測試結果可能恰恰相反。因此,為了得到有說服力的結論,我們需要測試各種規模的輸入資料,而這需要耗費大量的計算資源。
@ -32,8 +32,9 @@
- “隨著輸入資料大小的增加”意味著複雜度反映了演算法執行效率與輸入資料體量之間的關係。
- “時間和空間的增長趨勢”表示複雜度分析關注的不是執行時間或佔用空間的具體值,而是時間或空間增長的“快慢”。
**複雜度分析克服了實際測試方法的弊端**,體現在以下個方面。
**複雜度分析克服了實際測試方法的弊端**,體現在以下個方面。
- 它無需實際執行程式碼,更加綠色節能。
- 它獨立於測試環境,分析結果適用於所有執行平臺。
- 它可以體現不同資料量下的演算法效率,尤其是在大資料量下的演算法效能。

View File

@ -534,7 +534,7 @@ $$
- **時間複雜度能夠有效評估演算法效率**。例如,演算法 `B` 的執行時間呈線性增長,在 $n > 1$ 時比演算法 `A` 更慢,在 $n > 1000000$ 時比演算法 `C` 更慢。事實上,只要輸入資料大小 $n$ 足夠大,複雜度為“常數階”的演算法一定優於“線性階”的演算法,這正是時間增長趨勢的含義。
- **時間複雜度的推算方法更簡便**。顯然,執行平臺和計算操作型別都與演算法執行時間的增長趨勢無關。因此在時間複雜度分析中,我們可以簡單地將所有計算操作的執行時間視為相同的“單位時間”,從而將“計算操作執行時間統計”簡化為“計算操作數量統計”,這樣一來估算難度就大大降低了。
- **時間複雜度也存在一定的侷限性**。例如,儘管演算法 `A``C` 的時間複雜度相同,但實際執行時間差別很大。同樣,儘管演算法 `B` 的時間複雜度比 `C` 高,但在輸入資料大小 $n$ 較小時,演算法 `B` 明顯優於演算法 `C`在這些情況下,我們很難僅憑時間複雜度判斷演算法效率的高低。當然,儘管存在上述問題,複雜度分析仍然是評判演算法效率最有效且常用的方法。
- **時間複雜度也存在一定的侷限性**。例如,儘管演算法 `A``C` 的時間複雜度相同,但實際執行時間差別很大。同樣,儘管演算法 `B` 的時間複雜度比 `C` 高,但在輸入資料大小 $n$ 較小時,演算法 `B` 明顯優於演算法 `C`對於此類情況,我們時常難以僅憑時間複雜度判斷演算法效率的高低。當然,儘管存在上述問題,複雜度分析仍然是評判演算法效率最有效且常用的方法。
## 函式漸近上界

View File

@ -35,7 +35,7 @@
**Q**:原碼轉二補數的方法是“先取反後加 1”那麼二補數轉原碼應該是逆運算“先減 1 後取反”,而二補數轉原碼也一樣可以透過“先取反後加 1”得到這是為什麼呢
**A**這是因為原碼和二補數的相互轉換實際上是計算“補數”的過程。我們先給出補數的定義:假設 $a + b = c$ ,那麼我們稱 $a$ 是 $b$ 到 $c$ 的補數,反之也稱 $b$ 是 $a$ 到 $c$ 的補數。
這是因為原碼和二補數的相互轉換實際上是計算“補數”的過程。我們先給出補數的定義:假設 $a + b = c$ ,那麼我們稱 $a$ 是 $b$ 到 $c$ 的補數,反之也稱 $b$ 是 $a$ 到 $c$ 的補數。
給定一個 $n = 4$ 位長度的二進位制數 $0010$ ,如果將這個數字看作原碼(不考慮符號位),那麼它的二補數需透過“先取反後加 1”得到
@ -63,4 +63,4 @@ $$
本質上看,“取反”操作實際上是求到 $1111$ 的補數(因為恆有 `原碼 + 一補數 = 1111`);而在一補數基礎上再加 1 得到的二補數,就是到 $10000$ 的補數。
上述 $n = 4$ 為例,其可推廣至任意位數的二進位制數。
上述 $n = 4$ 為例,其可推廣至任意位數的二進位制數。

View File

@ -405,7 +405,7 @@
=== "Ruby"
```ruby title="heap.rb"
# Ruby 未提供內建 Heap 類別
```
=== "Zig"