Feature/chapter dynamic programming swift (#608)

* feat: add Swift codes for intro_to_dynamic_programming article

* feat: add Swift codes for dp_problem_features article

* feat: add Swift codes for dp_solution_pipeline article

* feat: add Swift codes for knapsack_problem article

* feat: add Swift codes for unbounded_knapsack_problem article

* feat: add Swift codes for edit_distance_problem article
pull/629/head^2
nuomi1 1 year ago committed by GitHub
parent 34985bdf2b
commit 9ea8a73059
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -72,6 +72,19 @@ let package = Package(
.executable(name: "subset_sum_i", targets: ["subset_sum_i"]),
.executable(name: "subset_sum_ii", targets: ["subset_sum_ii"]),
.executable(name: "n_queens", targets: ["n_queens"]),
// chapter_dynamic_programming
.executable(name: "climbing_stairs_backtrack", targets: ["climbing_stairs_backtrack"]),
.executable(name: "climbing_stairs_dfs", targets: ["climbing_stairs_dfs"]),
.executable(name: "climbing_stairs_dfs_mem", targets: ["climbing_stairs_dfs_mem"]),
.executable(name: "climbing_stairs_dp", targets: ["climbing_stairs_dp"]),
.executable(name: "min_cost_climbing_stairs_dp", targets: ["min_cost_climbing_stairs_dp"]),
.executable(name: "climbing_stairs_constraint_dp", targets: ["climbing_stairs_constraint_dp"]),
.executable(name: "min_path_sum", targets: ["min_path_sum"]),
.executable(name: "knapsack", targets: ["knapsack"]),
.executable(name: "unbounded_knapsack", targets: ["unbounded_knapsack"]),
.executable(name: "coin_change", targets: ["coin_change"]),
.executable(name: "coin_change_ii", targets: ["coin_change_ii"]),
.executable(name: "edit_distance", targets: ["edit_distance"]),
],
targets: [
// helper
@ -144,5 +157,18 @@ let package = Package(
.executableTarget(name: "subset_sum_i", path: "chapter_backtracking", sources: ["subset_sum_i.swift"]),
.executableTarget(name: "subset_sum_ii", path: "chapter_backtracking", sources: ["subset_sum_ii.swift"]),
.executableTarget(name: "n_queens", path: "chapter_backtracking", sources: ["n_queens.swift"]),
// chapter_dynamic_programming
.executableTarget(name: "climbing_stairs_backtrack", path: "chapter_dynamic_programming", sources: ["climbing_stairs_backtrack.swift"]),
.executableTarget(name: "climbing_stairs_dfs", path: "chapter_dynamic_programming", sources: ["climbing_stairs_dfs.swift"]),
.executableTarget(name: "climbing_stairs_dfs_mem", path: "chapter_dynamic_programming", sources: ["climbing_stairs_dfs_mem.swift"]),
.executableTarget(name: "climbing_stairs_dp", path: "chapter_dynamic_programming", sources: ["climbing_stairs_dp.swift"]),
.executableTarget(name: "min_cost_climbing_stairs_dp", path: "chapter_dynamic_programming", sources: ["min_cost_climbing_stairs_dp.swift"]),
.executableTarget(name: "climbing_stairs_constraint_dp", path: "chapter_dynamic_programming", sources: ["climbing_stairs_constraint_dp.swift"]),
.executableTarget(name: "min_path_sum", path: "chapter_dynamic_programming", sources: ["min_path_sum.swift"]),
.executableTarget(name: "knapsack", path: "chapter_dynamic_programming", sources: ["knapsack.swift"]),
.executableTarget(name: "unbounded_knapsack", path: "chapter_dynamic_programming", sources: ["unbounded_knapsack.swift"]),
.executableTarget(name: "coin_change", path: "chapter_dynamic_programming", sources: ["coin_change.swift"]),
.executableTarget(name: "coin_change_ii", path: "chapter_dynamic_programming", sources: ["coin_change_ii.swift"]),
.executableTarget(name: "edit_distance", path: "chapter_dynamic_programming", sources: ["edit_distance.swift"]),
]
)

@ -0,0 +1,42 @@
/**
* File: climbing_stairs_backtrack.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {
// n 1
if state == n {
res[0] += 1
}
//
for choice in choices {
// n
if state + choice > n {
break
}
backtrack(choices: choices, state: state + choice, n: n, res: &res)
}
}
/* */
func climbingStairsBacktrack(n: Int) -> Int {
let choices = [1, 2] // 1 2
let state = 0 // 0
var res: [Int] = []
res.append(0) // 使 res[0]
backtrack(choices: choices, state: state, n: n, res: &res)
return res[0]
}
@main
enum ClimbingStairsBacktrack {
/* Driver Code */
static func main() {
let n = 9
let res = climbingStairsBacktrack(n: n)
print("\(n) 阶楼梯共有 \(res) 种方案")
}
}

@ -0,0 +1,36 @@
/**
* File: climbing_stairs_constraint_dp.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func climbingStairsConstraintDP(n: Int) -> Int {
if n == 1 || n == 2 {
return n
}
// dp
var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)
//
dp[1][1] = 1
dp[1][2] = 0
dp[2][1] = 0
dp[2][2] = 1
//
for i in stride(from: 3, through: n, by: 1) {
dp[i][1] = dp[i - 1][2]
dp[i][2] = dp[i - 2][1] + dp[i - 2][2]
}
return dp[n][1] + dp[n][2]
}
@main
enum ClimbingStairsConstraintDP {
/* Driver Code */
static func main() {
let n = 9
let res = climbingStairsConstraintDP(n: n)
print("\(n) 阶楼梯共有 \(res) 种方案")
}
}

@ -0,0 +1,32 @@
/**
* File: climbing_stairs_dfs.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func dfs(i: Int) -> Int {
// dp[1] dp[2]
if i == 1 || i == 2 {
return i
}
// dp[i] = dp[i-1] + dp[i-2]
let count = dfs(i: i - 1) + dfs(i: i - 2)
return count
}
/* */
func climbingStairsDFS(n: Int) -> Int {
dfs(i: n)
}
@main
enum ClimbingStairsDFS {
/* Driver Code */
static func main() {
let n = 9
let res = climbingStairsDFS(n: n)
print("\(n) 阶楼梯共有 \(res) 种方案")
}
}

@ -0,0 +1,40 @@
/**
* File: climbing_stairs_dfs_mem.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func dfs(i: Int, mem: inout [Int]) -> Int {
// dp[1] dp[2]
if i == 1 || i == 2 {
return i
}
// dp[i]
if mem[i] != -1 {
return mem[i]
}
// dp[i] = dp[i-1] + dp[i-2]
let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)
// dp[i]
mem[i] = count
return count
}
/* */
func climbingStairsDFSMem(n: Int) -> Int {
// mem[i] i -1
var mem = Array(repeating: -1, count: n + 1)
return dfs(i: n, mem: &mem)
}
@main
enum ClimbingStairsDFSMem {
/* Driver Code */
static func main() {
let n = 9
let res = climbingStairsDFSMem(n: n)
print("\(n) 阶楼梯共有 \(res) 种方案")
}
}

@ -0,0 +1,49 @@
/**
* File: climbing_stairs_dp.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func climbingStairsDP(n: Int) -> Int {
if n == 1 || n == 2 {
return n
}
// dp
var dp = Array(repeating: 0, count: n + 1)
//
dp[1] = 1
dp[2] = 2
//
for i in stride(from: 3, through: n, by: 1) {
dp[i] = dp[i - 1] + dp[i - 2]
}
return dp[n]
}
/* */
func climbingStairsDPComp(n: Int) -> Int {
if n == 1 || n == 2 {
return n
}
var a = 1
var b = 2
for _ in stride(from: 3, through: n, by: 1) {
(a, b) = (b, a + b)
}
return b
}
@main
enum ClimbingStairsDP {
/* Driver Code */
static func main() {
let n = 9
var res = climbingStairsDP(n: n)
print("\(n) 阶楼梯共有 \(res) 种方案")
res = climbingStairsDPComp(n: n)
print("\(n) 阶楼梯共有 \(res) 种方案")
}
}

@ -0,0 +1,69 @@
/**
* File: coin_change.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func coinChangeDP(coins: [Int], amt: Int) -> Int {
let n = coins.count
let MAX = amt + 1
// dp
var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)
//
for a in stride(from: 1, through: amt, by: 1) {
dp[0][a] = MAX
}
//
for i in stride(from: 1, through: n, by: 1) {
for a in stride(from: 1, through: amt, by: 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] != MAX ? dp[n][amt] : -1
}
/* */
func coinChangeDPComp(coins: [Int], amt: Int) -> Int {
let n = coins.count
let MAX = amt + 1
// dp
var dp = Array(repeating: MAX, count: amt + 1)
dp[0] = 0
//
for i in stride(from: 1, through: n, by: 1) {
for a in stride(from: 1, through: amt, by: 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] != MAX ? dp[amt] : -1
}
@main
enum CoinChange {
/* Driver Code */
static func main() {
let coins = [1, 2, 5]
let amt = 4
//
var res = coinChangeDP(coins: coins, amt: amt)
print("凑到目标金额所需的最少硬币数量为 \(res)")
//
res = coinChangeDPComp(coins: coins, amt: amt)
print("凑到目标金额所需的最少硬币数量为 \(res)")
}
}

@ -0,0 +1,67 @@
/**
* File: coin_change_ii.swift
* Created Time: 2023-07-16
* Author: nuomi1 (nuomi1@qq.com)
*/
/* II */
func coinChangeIIDP(coins: [Int], amt: Int) -> Int {
let n = coins.count
// dp
var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)
//
for i in stride(from: 0, through: n, by: 1) {
dp[i][0] = 1
}
//
for i in stride(from: 1, through: n, by: 1) {
for a in stride(from: 1, through: amt, by: 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]]
}
}
}
return dp[n][amt]
}
/* II */
func coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {
let n = coins.count
// dp
var dp = Array(repeating: 0, count: amt + 1)
dp[0] = 1
//
for i in stride(from: 1, through: n, by: 1) {
for a in stride(from: 1, through: amt, by: 1) {
if coins[i - 1] > a {
// i
dp[a] = dp[a]
} else {
// i
dp[a] = dp[a] + dp[a - coins[i - 1]]
}
}
}
return dp[amt]
}
@main
enum CoinChangeII {
/* Driver Code */
static func main() {
let coins = [1, 2, 5]
let amt = 5
//
var res = coinChangeIIDP(coins: coins, amt: amt)
print("凑出目标金额的硬币组合数量为 \(res)")
//
res = coinChangeIIDPComp(coins: coins, amt: amt)
print("凑出目标金额的硬币组合数量为 \(res)")
}
}

@ -0,0 +1,147 @@
/**
* File: edit_distance.swift
* Created Time: 2023-07-16
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func editDistanceDFS(s: String, t: String, i: Int, j: Int) -> Int {
// s t 0
if i == 0, j == 0 {
return 0
}
// s t
if i == 0 {
return j
}
// t s
if j == 0 {
return i
}
//
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
return editDistanceDFS(s: s, t: t, i: i - 1, j: j - 1)
}
// = + 1
let insert = editDistanceDFS(s: s, t: t, i: i, j: j - 1)
let delete = editDistanceDFS(s: s, t: t, i: i - 1, j: j)
let replace = editDistanceDFS(s: s, t: t, i: i - 1, j: j - 1)
//
return min(min(insert, delete), replace) + 1
}
/* */
func editDistanceDFSMem(s: String, t: String, mem: inout [[Int]], i: Int, j: Int) -> Int {
// s t 0
if i == 0, j == 0 {
return 0
}
// s t
if i == 0 {
return j
}
// t s
if j == 0 {
return i
}
//
if mem[i][j] != -1 {
return mem[i][j]
}
//
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
return editDistanceDFS(s: s, t: t, i: i - 1, j: j - 1)
}
// = + 1
let insert = editDistanceDFS(s: s, t: t, i: i, j: j - 1)
let delete = editDistanceDFS(s: s, t: t, i: i - 1, j: j)
let replace = editDistanceDFS(s: s, t: t, i: i - 1, j: j - 1)
//
mem[i][j] = min(min(insert, delete), replace) + 1
return mem[i][j]
}
/* */
func editDistanceDP(s: String, t: String) -> Int {
let n = s.utf8CString.count
let m = t.utf8CString.count
var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)
//
for i in stride(from: 1, through: n, by: 1) {
dp[i][0] = i
}
for j in stride(from: 1, through: m, by: 1) {
dp[0][j] = j
}
//
for i in stride(from: 1, through: n, by: 1) {
for j in stride(from: 1, through: m, by: 1) {
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
//
dp[i][j] = dp[i - 1][j - 1]
} else {
// = + 1
dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1
}
}
}
return dp[n][m]
}
/* */
func editDistanceDPComp(s: String, t: String) -> Int {
let n = s.utf8CString.count
let m = t.utf8CString.count
var dp = Array(repeating: 0, count: m + 1)
//
for j in stride(from: 1, through: m, by: 1) {
dp[j] = j
}
//
for i in stride(from: 1, through: n, by: 1) {
//
var leftup = dp[0] // dp[i-1, j-1]
dp[0] = i
//
for j in stride(from: 1, through: m, by: 1) {
let temp = dp[j]
if s.utf8CString[i - 1] == t.utf8CString[j - 1] {
//
dp[j] = leftup
} else {
// = + 1
dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1
}
leftup = temp // dp[i-1, j-1]
}
}
return dp[m]
}
@main
enum EditDistance {
/* Driver Code */
static func main() {
let s = "bag"
let t = "pack"
let n = s.utf8CString.count
let m = t.utf8CString.count
//
var res = editDistanceDFS(s: s, t: t, i: n, j: m)
print("\(s) 更改为 \(t) 最少需要编辑 \(res)")
//
var mem = Array(repeating: Array(repeating: -1, count: m + 1), count: n + 1)
res = editDistanceDFSMem(s: s, t: t, mem: &mem, i: n, j: m)
print("\(s) 更改为 \(t) 最少需要编辑 \(res)")
//
res = editDistanceDP(s: s, t: t)
print("\(s) 更改为 \(t) 最少需要编辑 \(res)")
//
res = editDistanceDPComp(s: s, t: t)
print("\(s) 更改为 \(t) 最少需要编辑 \(res)")
}
}

@ -0,0 +1,110 @@
/**
* File: knapsack.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* 0-1 */
func knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {
// 0
if i == 0 || c == 0 {
return 0
}
//
if wgt[i - 1] > c {
return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)
}
// i
let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)
let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]
//
return max(no, yes)
}
/* 0-1 */
func knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {
// 0
if i == 0 || c == 0 {
return 0
}
//
if mem[i][c] != -1 {
return mem[i][c]
}
//
if wgt[i - 1] > c {
return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)
}
// i
let no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)
let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]
//
mem[i][c] = max(no, yes)
return mem[i][c]
}
/* 0-1 */
func knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {
let n = wgt.count
// dp
var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)
//
for i in stride(from: 1, through: n, by: 1) {
for c in stride(from: 1, through: cap, by: 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 - 1][c - wgt[i - 1]] + val[i - 1])
}
}
}
return dp[n][cap]
}
/* 0-1 */
func knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {
let n = wgt.count
// dp
var dp = Array(repeating: 0, count: cap + 1)
//
for i in stride(from: 1, through: n, by: 1) {
//
for c in stride(from: cap, through: 1, by: -1) {
if wgt[i - 1] <= c {
// i
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])
}
}
}
return dp[cap]
}
@main
enum Knapsack {
/* Driver Code */
static func main() {
let wgt = [10, 20, 30, 40, 50]
let val = [50, 120, 150, 210, 240]
let cap = 50
let n = wgt.count
//
var res = knapsackDFS(wgt: wgt, val: val, i: n, c: cap)
print("不超过背包容量的最大物品价值为 \(res)")
//
var mem = Array(repeating: Array(repeating: -1, count: cap + 1), count: n + 1)
res = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: n, c: cap)
print("不超过背包容量的最大物品价值为 \(res)")
//
res = knapsackDP(wgt: wgt, val: val, cap: cap)
print("不超过背包容量的最大物品价值为 \(res)")
//
res = knapsackDPComp(wgt: wgt, val: val, cap: cap)
print("不超过背包容量的最大物品价值为 \(res)")
}
}

@ -0,0 +1,51 @@
/**
* File: min_cost_climbing_stairs_dp.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func minCostClimbingStairsDP(cost: [Int]) -> Int {
let n = cost.count - 1
if n == 1 || n == 2 {
return cost[n]
}
// dp
var dp = Array(repeating: 0, count: n + 1)
//
dp[1] = 1
dp[2] = 2
//
for i in stride(from: 3, through: n, by: 1) {
dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]
}
return dp[n]
}
/* */
func minCostClimbingStairsDPComp(cost: [Int]) -> Int {
let n = cost.count - 1
if n == 1 || n == 2 {
return cost[n]
}
var (a, b) = (cost[1], cost[2])
for i in stride(from: 3, through: n, by: 1) {
(a, b) = (b, min(a, b) + cost[i])
}
return b
}
@main
enum MinCostClimbingStairsDP {
/* Driver Code */
static func main() {
let cost = [0, 1, 10, 1, 1, 1, 10, 1, 1, 10, 1]
print("输入楼梯的代价列表为 \(cost)")
var res = minCostClimbingStairsDP(cost: cost)
print("爬完楼梯的最低代价为 \(res)")
res = minCostClimbingStairsDPComp(cost: cost)
print("爬完楼梯的最低代价为 \(res)")
}
}

@ -0,0 +1,123 @@
/**
* File: min_path_sum.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {
//
if i == 0, j == 0 {
return grid[0][0]
}
// +
if i < 0 || j < 0 {
return .max
}
// (i-1, j) (i, j-1)
let left = minPathSumDFS(grid: grid, i: i - 1, j: j)
let up = minPathSumDFS(grid: grid, i: i, j: j - 1)
// (i, j)
return min(left, up) + grid[i][j]
}
/* */
func minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {
//
if i == 0, j == 0 {
return grid[0][0]
}
// +
if i < 0 || j < 0 {
return .max
}
//
if mem[i][j] != -1 {
return mem[i][j]
}
//
let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)
let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)
// (i, j)
mem[i][j] = min(left, up) + grid[i][j]
return mem[i][j]
}
/* */
func minPathSumDP(grid: [[Int]]) -> Int {
let n = grid.count
let m = grid[0].count
// dp
var dp = Array(repeating: Array(repeating: 0, count: m), count: n)
dp[0][0] = grid[0][0]
//
for j in stride(from: 1, to: m, by: 1) {
dp[0][j] = dp[0][j - 1] + grid[0][j]
}
//
for i in stride(from: 1, to: n, by: 1) {
dp[i][0] = dp[i - 1][0] + grid[i][0]
}
//
for i in stride(from: 1, to: n, by: 1) {
for j in stride(from: 1, to: m, by: 1) {
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]
}
}
return dp[n - 1][m - 1]
}
/* */
func minPathSumDPComp(grid: [[Int]]) -> Int {
let n = grid.count
let m = grid[0].count
// dp
var dp = Array(repeating: 0, count: m)
//
dp[0] = grid[0][0]
for j in stride(from: 1, to: m, by: 1) {
dp[j] = dp[j - 1] + grid[0][j]
}
//
for i in stride(from: 1, to: n, by: 1) {
//
dp[0] = dp[0] + grid[i][0]
//
for j in stride(from: 1, to: m, by: 1) {
dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]
}
}
return dp[m - 1]
}
@main
enum MinPathSum {
/* Driver Code */
static func main() {
let grid = [
[1, 3, 1, 5],
[2, 2, 4, 2],
[5, 3, 2, 1],
[4, 3, 5, 2],
]
let n = grid.count
let m = grid[0].count
//
var res = minPathSumDFS(grid: grid, i: n - 1, j: m - 1)
print("从左上角到右下角的做小路径和为 \(res)")
//
var mem = Array(repeating: Array(repeating: -1, count: m), count: n)
res = minPathSumDFSMem(grid: grid, mem: &mem, i: n - 1, j: m - 1)
print("从左上角到右下角的做小路径和为 \(res)")
//
res = minPathSumDP(grid: grid)
print("从左上角到右下角的做小路径和为 \(res)")
//
res = minPathSumDPComp(grid: grid)
print("从左上角到右下角的做小路径和为 \(res)")
}
}

@ -0,0 +1,63 @@
/**
* File: unbounded_knapsack.swift
* Created Time: 2023-07-15
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {
let n = wgt.count
// dp
var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)
//
for i in stride(from: 1, through: n, by: 1) {
for c in stride(from: 1, through: cap, by: 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]
}
/* */
func unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {
let n = wgt.count
// dp
var dp = Array(repeating: 0, count: cap + 1)
//
for i in stride(from: 1, through: n, by: 1) {
for c in stride(from: 1, through: cap, by: 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]
}
@main
enum UnboundedKnapsack {
/* Driver Code */
static func main() {
let wgt = [1, 2, 3]
let val = [5, 11, 15]
let cap = 4
//
var res = unboundedKnapsackDP(wgt: wgt, val: val, cap: cap)
print("不超过背包容量的最大物品价值为 \(res)")
//
res = unboundedKnapsackDPComp(wgt: wgt, val: val, cap: cap)
print("不超过背包容量的最大物品价值为 \(res)")
}
}
Loading…
Cancel
Save