mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 14:18:20 +08:00 
			
		
		
		
	* feat(swift): review for chapter_computational_complexity * feat(swift): review for chapter_data_structure * feat(swift): review for chapter_array_and_linkedlist * feat(swift): review for chapter_stack_and_queue * feat(swift): review for chapter_hashing * feat(swift): review for chapter_tree * feat(swift): add codes for heap article * feat(swift): review for chapter_heap * feat(swift): review for chapter_graph * feat(swift): review for chapter_searching * feat(swift): review for chapter_sorting * feat(swift): review for chapter_divide_and_conquer * feat(swift): review for chapter_backtracking * feat(swift): review for chapter_dynamic_programming * feat(swift): review for chapter_greedy * feat(swift): review for utils * feat(swift): update ci tool * feat(swift): trailing closure * feat(swift): array init * feat(swift): map index
		
			
				
	
	
		
			39 lines
		
	
	
		
			847 B
		
	
	
	
		
			Swift
		
	
	
	
	
	
			
		
		
	
	
			39 lines
		
	
	
		
			847 B
		
	
	
	
		
			Swift
		
	
	
	
	
	
/**
 | 
						||
 * 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)")
 | 
						||
    }
 | 
						||
}
 |