11. Container With Most Water

package LeetCode_11

/**
 * 11. Container With Most Water
 * https://leetcode.com/problems/container-with-most-water/description/
 * */
class Solution {
    fun maxArea(height: IntArray): Int {
        var area = 0
        var left = 0
        var right = height.size - 1
        while (left < right) {
            //根据木桶原理(Cannikin Law),面积是决定在shortest plank
            val h = Math.min(height[left], height[right])
            val currentArea = h * (right - left)
            area = Math.max(area, currentArea)
            if (height[left] < height[right]) {
                left++
            } else {
                right--
            }
        }
        return area
    }
}
原文地址:https://www.cnblogs.com/johnnyzhao/p/12697103.html