{ "question_id": 11, "question_title": "盛最多水的容器", "difficulty": "中等", "question_content": "<p>给你 <code>n</code> 个非负整数 <code>a<sub>1</sub>,a<sub>2,</sub>...,a</code><sub><code>n</code>,</sub>每个数代表坐标中的一个点 <code>(i, a<sub>i</sub>)</code> 。在坐标内画 <code>n</code> 条垂直线,垂直线 <code>i</code> 的两个端点分别为 <code>(i, a<sub>i</sub>)</code> 和 <code>(i, 0)</code> 。找出其中的两条线,使得它们与 <code>x</code> 轴共同构成的容器可以容纳最多的水。</p><p><strong>说明:</strong>你不能倾斜容器。</p><p> </p><p><strong>示例 1:</strong></p><p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0011.Container%20With%20Most%20Water/images/question_11.jpg\" style=\"height: 287px; width: 600px;\" /></p><pre><strong>输入:</strong>[1,8,6,2,5,4,8,3,7]<strong><br />输出:</strong>49 <strong><br />解释:</strong>图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>height = [1,1]<strong><br />输出:</strong>1</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>height = [4,3,2,1,4]<strong><br />输出:</strong>16</pre><p><strong>示例 4:</strong></p><pre><strong>输入:</strong>height = [1,2,1]<strong><br />输出:</strong>2</pre><p> </p><p><strong>提示:</strong></p><ul>\t<li><code>n = height.length</code></li>\t<li><code>2 <= n <= 3 * 10<sup>4</sup></code></li>\t<li><code>0 <= height[i] <= 3 * 10<sup>4</sup></code></li></ul>", "topic_link": "https://bbs.csdn.net/topics/600469816", "cpp": "#define MAX(a, b) (((a) < (b)) ? (b) : (a))\n#define MIN(a, b) (((a) > (b)) ? (b) : (a))\nint maxArea(int *height, int heightSize)\n{\n\tint max = 0;\n\tint i = 0, j = heightSize - 1;\n\tint a;\n\twhile (i < j)\n\t{\n\t\ta = MIN(height[i], height[j]) * (j - i);\n\t\tmax = MAX(max, a);\n\t\tif (height[i] > height[j])\n\t\t\t--j;\n\t\telse\n\t\t\t++i;\n\t}\n\treturn max;\n}", "java": "class Solution {\n\tpublic int maxArea(int[] height) {\n\t\tint N = height.length;\n\t\tint i = 0;\n\t\tint j = N - 1;\n\t\tint max = 0;\n\t\twhile (i < j) {\n\t\t\tint c = (j - i) * Math.min(height[i], height[j]);\n\t\t\tif (c > max) {\n\t\t\t\tmax = c;\n\t\t\t}\n\t\t\tif (height[i] > height[j]) {\n\t\t\t\tj--;\n\t\t\t} else {\n\t\t\t\ti++;\n\t\t\t}\n\t\t}\n\t\treturn max;\n\t}\n}", "js": "/**\n * @param {number[]} height\n * @return {number}\n */\nvar maxArea = function(height) {\n let N = height.length;\n \n let i=0;\n let j=N-1;\n let max=0;\n while(i<j){\n let c = (j-i)*Math.min(height[i],height[j]);\n if(c>max){\n max = c;\n }\n if(height[i]>height[j]){\n j--;\n }else{\n i++;\n }\n }\n return max;\n};\n", "python": "from typing import List\nclass Solution:\n\tdef maxArea(self, height: List[int]) -> int:\n\t\tN = len(height)\n\t\ti = 0\n\t\tj = N-1\n\t\tmax_area = 0\n\t\twhile i < j:\n\t\t\tc = (j-i)*min(height[i], height[j])\n\t\t\tif c > max_area:\n\t\t\t\tmax_area = c\n\t\t\tif height[i] > height[j]:\n\t\t\t\tj -= 1\n\t\t\telse:\n\t\t\t\ti += 1\n\t\treturn max_area\n# %%\ns = Solution()\nprint(s.maxArea([1,8,6,2,5,4,8,3,7]))", "status": 1, "keywords": "贪心,数组,双指针", "license": { "cpp": "csdn.net", "python": "csdn.net", "java": "csdn.net" }, "notebook": { "cpp": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/10/10_cpp.ipynb?type=file", "python": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/10/10_python.ipynb?type=file", "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/leetcode/ipynb/10/10_java.ipynb?type=file" }, "notebook_enable": 1 }