You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hello-algo/codes/swift/chapter_greedy/max_capacity.swift

39 lines
847 B

/**
* File: max_capacity.swift
* Created Time: 2023-09-03
* Author: nuomi1 (nuomi1@qq.com)
*/
/* */
func maxCapacity(ht: [Int]) -> Int {
// i, j使
var i = ht.startIndex, j = ht.endIndex - 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)")
}
}