@ -0,0 +1,70 @@
|
||||
/**
|
||||
* File: coin_change.cpp
|
||||
* Created Time: 2023-07-11
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 零钱兑换:动态规划 */
|
||||
int coinChangeDP(vector<int> &coins, int amt) {
|
||||
int n = coins.size();
|
||||
int MAX = amt + 1;
|
||||
// 初始化 dp 表
|
||||
vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));
|
||||
// 状态转移:首行首列
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
dp[0][a] = MAX;
|
||||
}
|
||||
// 状态转移:其余行列
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超过背包容量,则不选硬币 i
|
||||
dp[i][a] = dp[i - 1][a];
|
||||
} else {
|
||||
// 不选和选硬币 i 这两种方案的较小值
|
||||
dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n][amt] != MAX ? dp[n][amt] : -1;
|
||||
}
|
||||
|
||||
/* 零钱兑换:状态压缩后的动态规划 */
|
||||
int coinChangeDPComp(vector<int> &coins, int amt) {
|
||||
int n = coins.size();
|
||||
int MAX = amt + 1;
|
||||
// 初始化 dp 表
|
||||
vector<int> dp(amt + 1, MAX);
|
||||
dp[0] = 0;
|
||||
// 状态转移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超过背包容量,则不选硬币 i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// 不选和选硬币 i 这两种方案的较小值
|
||||
dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amt] != MAX ? dp[amt] : -1;
|
||||
}
|
||||
|
||||
/* Driver code */
|
||||
int main() {
|
||||
vector<int> coins = {1, 2, 5};
|
||||
int amt = 4;
|
||||
|
||||
// 动态规划
|
||||
int res = coinChangeDP(coins, amt);
|
||||
cout << "凑到目标金额所需的最少硬币数量为 " << res << endl;
|
||||
|
||||
// 状态压缩后的动态规划
|
||||
res = coinChangeDPComp(coins, amt);
|
||||
cout << "凑到目标金额所需的最少硬币数量为 " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
/**
|
||||
* File: unbounded_knapsack.cpp
|
||||
* Created Time: 2023-07-11
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 完全背包:动态规划 */
|
||||
int unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {
|
||||
int n = wgt.size();
|
||||
// 初始化 dp 表
|
||||
vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));
|
||||
// 状态转移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// 若超过背包容量,则不选物品 i
|
||||
dp[i][c] = dp[i - 1][c];
|
||||
} else {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n][cap];
|
||||
}
|
||||
|
||||
/* 完全背包:状态压缩后的动态规划 */
|
||||
int unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {
|
||||
int n = wgt.size();
|
||||
// 初始化 dp 表
|
||||
vector<int> dp(cap + 1, 0);
|
||||
// 状态转移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// 若超过背包容量,则不选物品 i
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[cap];
|
||||
}
|
||||
|
||||
/* Driver code */
|
||||
int main() {
|
||||
vector<int> wgt = {1, 2, 3};
|
||||
vector<int> val = {5, 11, 15};
|
||||
int cap = 4;
|
||||
|
||||
// 动态规划
|
||||
int res = unboundedKnapsackDP(wgt, val, cap);
|
||||
cout << "不超过背包容量的最大物品价值为 " << res << endl;
|
||||
|
||||
// 状态压缩后的动态规划
|
||||
res = unboundedKnapsackDPComp(wgt, val, cap);
|
||||
cout << "不超过背包容量的最大物品价值为 " << res << endl;
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/**
|
||||
* File: coin_change.java
|
||||
* Created Time: 2023-07-11
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_dynamic_programming;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class coin_change {
|
||||
/* 零钱兑换:动态规划 */
|
||||
static int coinChangeDP(int[] coins, int amt) {
|
||||
int n = coins.length;
|
||||
int MAX = amt + 1;
|
||||
// 初始化 dp 表
|
||||
int[][] dp = new int[n + 1][amt + 1];
|
||||
// 状态转移:首行首列
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
dp[0][a] = MAX;
|
||||
}
|
||||
// 状态转移:其余行列
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超过背包容量,则不选硬币 i
|
||||
dp[i][a] = dp[i - 1][a];
|
||||
} else {
|
||||
// 不选和选硬币 i 这两种方案的较小值
|
||||
dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n][amt] != MAX ? dp[n][amt] : -1;
|
||||
}
|
||||
|
||||
/* 零钱兑换:状态压缩后的动态规划 */
|
||||
static int coinChangeDPComp(int[] coins, int amt) {
|
||||
int n = coins.length;
|
||||
int MAX = amt + 1;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[amt + 1];
|
||||
Arrays.fill(dp, MAX);
|
||||
dp[0] = 0;
|
||||
// 状态转移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int a = 1; a <= amt; a++) {
|
||||
if (coins[i - 1] > a) {
|
||||
// 若超过背包容量,则不选硬币 i
|
||||
dp[a] = dp[a];
|
||||
} else {
|
||||
// 不选和选硬币 i 这两种方案的较小值
|
||||
dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[amt] != MAX ? dp[amt] : -1;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] coins = { 1, 2, 5 };
|
||||
int amt = 4;
|
||||
|
||||
// 动态规划
|
||||
int res = coinChangeDP(coins, amt);
|
||||
System.out.println("凑到目标金额所需的最少硬币数量为 " + res);
|
||||
|
||||
// 状态压缩后的动态规划
|
||||
res = coinChangeDPComp(coins, amt);
|
||||
System.out.println("凑到目标金额所需的最少硬币数量为 " + res);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/**
|
||||
* File: unbounded_knapsack.java
|
||||
* Created Time: 2023-07-11
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* 完全背包:动态规划 */
|
||||
static int unboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// 初始化 dp 表
|
||||
int[][] dp = new int[n + 1][cap + 1];
|
||||
// 状态转移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// 若超过背包容量,则不选物品 i
|
||||
dp[i][c] = dp[i - 1][c];
|
||||
} else {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[n][cap];
|
||||
}
|
||||
|
||||
/* 完全背包:状态压缩后的动态规划 */
|
||||
static int unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.length;
|
||||
// 初始化 dp 表
|
||||
int[] dp = new int[cap + 1];
|
||||
// 状态转移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// 若超过背包容量,则不选物品 i
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[cap];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] wgt = { 1, 2, 3 };
|
||||
int[] val = { 5, 11, 15 };
|
||||
int cap = 4;
|
||||
|
||||
// 动态规划
|
||||
int res = unboundedKnapsackDP(wgt, val, cap);
|
||||
System.out.println("不超过背包容量的最大物品价值为 " + res);
|
||||
|
||||
// 状态压缩后的动态规划
|
||||
res = unboundedKnapsackDPComp(wgt, val, cap);
|
||||
System.out.println("不超过背包容量的最大物品价值为 " + res);
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
"""
|
||||
File: coin_change.py
|
||||
Created Time: 2023-07-10
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def coin_change_dp(coins: list[int], amt: int) -> int:
|
||||
"""零钱兑换:动态规划"""
|
||||
n = len(coins)
|
||||
MAX = amt + 1
|
||||
# 初始化 dp 表
|
||||
dp = [[0] * (amt + 1) for _ in range(n + 1)]
|
||||
# 状态转移:首行首列
|
||||
for a in range(1, amt + 1):
|
||||
dp[0][a] = MAX
|
||||
# 状态转移:其余行列
|
||||
for i in range(1, n + 1):
|
||||
for a in range(1, amt + 1):
|
||||
if coins[i - 1] > a:
|
||||
# 若超过背包容量,则不选硬币 i
|
||||
dp[i][a] = dp[i - 1][a]
|
||||
else:
|
||||
# 不选和选硬币 i 这两种方案的较小值
|
||||
dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)
|
||||
return dp[n][amt] if dp[n][amt] != MAX else -1
|
||||
|
||||
|
||||
def coin_change_dp_comp(coins: list[int], amt: int) -> int:
|
||||
"""零钱兑换:状态压缩后的动态规划"""
|
||||
n = len(coins)
|
||||
MAX = amt + 1
|
||||
# 初始化 dp 表
|
||||
dp = [MAX] * (amt + 1)
|
||||
dp[0] = 0
|
||||
# 状态转移
|
||||
for i in range(1, n + 1):
|
||||
# 正序遍历
|
||||
for a in range(1, amt + 1):
|
||||
if coins[i - 1] > a:
|
||||
# 若超过背包容量,则不选硬币 i
|
||||
dp[a] = dp[a]
|
||||
else:
|
||||
# 不选和选硬币 i 这两种方案的较小值
|
||||
dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)
|
||||
return dp[amt] if dp[amt] != MAX else -1
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
coins = [1, 2, 5]
|
||||
amt = 4
|
||||
|
||||
# 动态规划
|
||||
res = coin_change_dp(coins, amt)
|
||||
print(f"凑到目标金额所需的最少硬币数量为 {res}")
|
||||
|
||||
# 状态压缩后的动态规划
|
||||
res = coin_change_dp_comp(coins, amt)
|
||||
print(f"凑到目标金额所需的最少硬币数量为 {res}")
|
@ -0,0 +1,55 @@
|
||||
"""
|
||||
File: unbounded_knapsack.py
|
||||
Created Time: 2023-07-10
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:
|
||||
"""完全背包:动态规划"""
|
||||
n = len(wgt)
|
||||
# 初始化 dp 表
|
||||
dp = [[0] * (cap + 1) for _ in range(n + 1)]
|
||||
# 状态转移
|
||||
for i in range(1, n + 1):
|
||||
for c in range(1, cap + 1):
|
||||
if wgt[i - 1] > c:
|
||||
# 若超过背包容量,则不选物品 i
|
||||
dp[i][c] = dp[i - 1][c]
|
||||
else:
|
||||
# 不选和选物品 i 这两种方案的较大值
|
||||
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])
|
||||
return dp[n][cap]
|
||||
|
||||
|
||||
def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:
|
||||
"""完全背包:状态压缩后的动态规划"""
|
||||
n = len(wgt)
|
||||
# 初始化 dp 表
|
||||
dp = [0] * (cap + 1)
|
||||
# 状态转移
|
||||
for i in range(1, n + 1):
|
||||
# 正序遍历
|
||||
for c in range(1, cap + 1):
|
||||
if wgt[i - 1] > c:
|
||||
# 若超过背包容量,则不选物品 i
|
||||
dp[c] = dp[c]
|
||||
else:
|
||||
# 不选和选物品 i 这两种方案的较大值
|
||||
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])
|
||||
return dp[cap]
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
wgt = [1, 2, 3]
|
||||
val = [5, 11, 15]
|
||||
cap = 4
|
||||
|
||||
# 动态规划
|
||||
res = unbounded_knapsack_dp(wgt, val, cap)
|
||||
print(f"不超过背包容量的最大物品价值为 {res}")
|
||||
|
||||
# 状态压缩后的动态规划
|
||||
res = unbounded_knapsack_dp_comp(wgt, val, cap)
|
||||
print(f"不超过背包容量的最大物品价值为 {res}")
|
After Width: | Height: | Size: 61 KiB |
After Width: | Height: | Size: 65 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 57 KiB |
After Width: | Height: | Size: 63 KiB |
After Width: | Height: | Size: 65 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 64 KiB |
After Width: | Height: | Size: 65 KiB |
After Width: | Height: | Size: 65 KiB |
After Width: | Height: | Size: 54 KiB |
After Width: | Height: | Size: 62 KiB |
After Width: | Height: | Size: 65 KiB |
After Width: | Height: | Size: 66 KiB |
After Width: | Height: | Size: 67 KiB |
After Width: | Height: | Size: 68 KiB |
After Width: | Height: | Size: 67 KiB |
After Width: | Height: | Size: 67 KiB |
After Width: | Height: | Size: 69 KiB |