1. 程式人生 > 資訊 >聯想再預熱摩托羅拉新機:新基帶很給力,360° 無死角訊號強

聯想再預熱摩托羅拉新機:新基帶很給力,360° 無死角訊號強

給你 n 個非負整數 a1,a2,...,an,每個數代表座標中的一個點(i,ai) 。在座標內畫 n 條垂直線,垂直線 i的兩個端點分別為(i,ai) 和 (i, 0) 。找出其中的兩條線,使得它們與x軸共同構成的容器可以容納最多的水。

說明:你不能傾斜容器。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/container-with-most-water
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

class Solution {
    public static int maxArea(int[] height) {
        if (height == null || height.length <= 1) {
            return 0;
        }

        int ret = 0;
        int leftIndex = 0, rightIndex = height.length - 1;

        while (leftIndex < rightIndex) {
            ret = Math.max(ret, Math.min(height[leftIndex], height[rightIndex]) * (rightIndex - leftIndex));
            if (height[leftIndex] < height[rightIndex]) {
                leftIndex++;
            } else {
                rightIndex--;
            }
        }
        return ret;
    }
}
心之所向,素履以往 生如逆旅,一葦以航