【Leetcode】11. 盛最多水的容器(Container With Most Water)

Leetcode - 11 Container With Most Water (Medium)

題目描述:找出兩條線,使得構成的容器能夠容納最多的水。


public int maxArea(int[] height) {
    int max = 0, i = 0, j = height.length - 1;
    while (i < j) {
        max = Math.max(max, (j - i) * Math.min(height[i], height[j]));
        if (height[i] < height[j]) {
            i++;
        } else {
            j--;
        }
    }
    return max;
}
發佈了212 篇原創文章 · 獲贊 43 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章