Feature/chapter greedy swift (#720)

* feat: add Swift codes for greedy_algorithm article

* feat: add Swift codes for fractional_knapsack_problem article

* feat: add Swift codes for max_capacity_problem article

* feat: add Swift codes for max_product_cutting_problem article
pull/721/head
nuomi1 1 year ago committed by GitHub
parent dd72335235
commit 8d5e84f70a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -93,6 +93,11 @@ let package = Package(
.executable(name: "coin_change", targets: ["coin_change"]),
.executable(name: "coin_change_ii", targets: ["coin_change_ii"]),
.executable(name: "edit_distance", targets: ["edit_distance"]),
// chapter_greedy
.executable(name: "coin_change_greedy", targets: ["coin_change_greedy"]),
.executable(name: "fractional_knapsack", targets: ["fractional_knapsack"]),
.executable(name: "max_capacity", targets: ["max_capacity"]),
.executable(name: "max_product_cutting", targets: ["max_product_cutting"]),
],
targets: [
// helper
@ -187,5 +192,10 @@ let package = Package(
.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"]),
// chapter_greedy
.executableTarget(name: "coin_change_greedy", path: "chapter_greedy", sources: ["coin_change_greedy.swift"]),
.executableTarget(name: "fractional_knapsack", path: "chapter_greedy", sources: ["fractional_knapsack.swift"]),
.executableTarget(name: "max_capacity", path: "chapter_greedy", sources: ["max_capacity.swift"]),
.executableTarget(name: "max_product_cutting", path: "chapter_greedy", sources: ["max_product_cutting.swift"]),
]
)

@ -0,0 +1,54 @@
/**
* File: coin_change_greedy.swift
* Created Time: 2023-09-03
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func coinChangeGreedy(coins: [Int], amt: Int) -> Int {
// coins
var i = coins.count - 1
var count = 0
var amt = amt
//
while amt > 0 {
//
while i > 0 && coins[i] > amt {
i -= 1
}
// coins[i]
amt -= coins[i]
count += 1
}
// -1
return amt == 0 ? count : -1
}
@main
enum CoinChangeGreedy {
/* Driver Code */
static func main() {
//
var coins = [1, 5, 10, 20, 50, 100]
var amt = 186
var res = coinChangeGreedy(coins: coins, amt: amt)
print("\ncoins = \(coins), amount = \(amt)")
print("凑到 \(amt) 所需的最少硬币数量为 \(res)")
//
coins = [1, 20, 50]
amt = 60
res = coinChangeGreedy(coins: coins, amt: amt)
print("\ncoins = \(coins), amount = \(amt)")
print("凑到 \(amt) 所需的最少硬币数量为 \(res)")
print("实际上需要的最少数量为 3 ,即 20 + 20 + 20")
//
coins = [1, 49, 50]
amt = 98
res = coinChangeGreedy(coins: coins, amt: amt)
print("\ncoins = \(coins), amount = \(amt)")
print("凑到 \(amt) 所需的最少硬币数量为 \(res)")
print("实际上需要的最少数量为 2 ,即 49 + 49")
}
}

@ -0,0 +1,57 @@
/**
* File: fractional_knapsack.swift
* Created Time: 2023-09-03
* Author: nuomi1 (nuomi1@qq.com)
*/
//
struct Item {
var w: Int //
var v: Int //
init(w: Int, v: Int) {
self.w = w
self.v = v
}
}
/* */
func fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {
//
var items = zip(wgt, val).map { Item(w: $0, v: $1) }
// item.v / item.w
items.sort(by: { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) })
//
var res = 0.0
var cap = cap
for item in items {
if item.w <= cap {
//
res += Double(item.v)
cap -= item.w
} else {
//
res += Double(item.v) / Double(item.w) * Double(cap)
//
break
}
}
return res
}
@main
enum FractionalKnapsack {
/* Driver Code */
static func main() {
//
let wgt = [10, 20, 30, 40, 50]
//
let val = [50, 120, 150, 210, 240]
//
let cap = 50
//
let res = fractionalKnapsack(wgt: wgt, val: val, cap: cap)
print("不超过背包容量的最大物品价值为 \(res)")
}
}

@ -0,0 +1,38 @@
/**
* File: max_capacity.swift
* Created Time: 2023-09-03
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func maxCapacity(ht: [Int]) -> Int {
// i, j
var i = 0, j = ht.count - 1
// 0
var res = 0
//
while i < j {
//
let cap = min(ht[i], ht[j]) * (j - i)
res = max(res, cap)
//
if ht[i] < ht[j] {
i += 1
} else {
j -= 1
}
}
return res
}
@main
enum MaxCapacity {
/* Driver Code */
static func main() {
let ht = [3, 8, 5, 2, 7, 7, 3, 4]
//
let res = maxCapacity(ht: ht)
print("最大容量为 \(res)")
}
}

@ -0,0 +1,43 @@
/**
* File: max_product_cutting.swift
* Created Time: 2023-09-03
* Author: nuomi1 (nuomi1@qq.com)
*/
import Foundation
func pow(_ x: Int, _ y: Int) -> Int {
Int(Double(truncating: pow(Decimal(x), y) as NSDecimalNumber))
}
/* */
func maxProductCutting(n: Int) -> Int {
// n <= 3 1
if n <= 3 {
return 1 * (n - 1)
}
// 3 a 3 b
let a = n / 3
let b = n % 3
if b == 1 {
// 1 1 * 3 2 * 2
return pow(3, a - 1) * 2 * 2
}
if b == 2 {
// 2
return pow(3, a) * 2
}
// 0
return pow(3, a)
}
@main
enum MaxProductCutting {
static func main() {
let n = 58
//
let res = maxProductCutting(n: n)
print("最大切分乘积为 \(res)")
}
}
Loading…
Cancel
Save