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
832 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/**
* 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)")
}
}