【LeetCode】11. 盛最多水的容器

93 阅读1分钟

Description

Array Two Pointers

URL: leetcode-cn.com/problems/co…


Python

class Solution:
    def maxArea(self, height: List[int]) -> int:
        i, j = 0, len(height) - 1
        max_area = 0
        while i < j:
            area = (j - i) * min(height[i], height[j])
            max_area = area if area > max_area else max_area
            if height[i] > height[j]:
                j -= 1
            else:
                i += 1
        return max_area

Java

class Solution {
    public int maxArea(int[] height) {
        int max = 0;
        for (int i = 0, j = height.length - 1; i < j; ) {
            int minHeight = height[i] < height[j] ? height[i++] : height[j--];
            max = Math.max(max, (j - i + 1) * minHeight);
        }
        return max;
    }
}

PHP

class Solution {
    /**
     * @param Integer[] $height
     * @return Integer
     */
    function maxArea($height) {
        $max = 0;
        for ($i = 0, $j = count($height) - 1; $i < $j; ) {
            $min_height = $height[$i] < $height[$j] ? $height[$i++] : $height[$j--];
            $max = max($max, ($j - $i + 1) * $min_height);
        }
        return $max;
    }
}

END φ(..。)