盛最多水的容器

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

說明:你不能傾斜容器,且 n 的值至少爲 2。spa

 

 

圖中垂直線表明輸入數組 [1,8,6,2,5,4,8,3,7]。在此狀況下,容器可以容納水(表示爲藍色部分)的最大值爲 49。code

 

示例:blog

輸入: [1,8,6,2,5,4,8,3,7]
輸出: 49leetcode

來源:力扣(LeetCode)
連接:https://leetcode-cn.com/problems/container-with-most-waterit

 public int MaxArea(int[] height)
        {           
            int max = 0;
            for (int i = 0; i < height.Length ; i++)
            {
                for (int j = i + 1; j < height.Length; j++)
                {
                    max = Math.Max(max, Math.Min(height[i] ,height[j]) * (j - i));

                }
            }

            return max;
        }
相關文章
相關標籤/搜索