Nav: << previous: 10.正则表达式匹配 | next: 12.整数转罗马数字 >>


Description

tab: English
 
<p>You are given an integer array <code>height</code> of length <code>n</code>. There are <code>n</code> vertical lines drawn such that the two endpoints of the <code>i<sup>th</sup></code> line are <code>(i, 0)</code> and <code>(i, height[i])</code>.</p>
 
<p>Find two lines that together with the x-axis form a container, such that the container contains the most water.</p>
 
<p>Return <em>the maximum amount of water a container can store</em>.</p>
 
<p><strong>Notice</strong> that you may not slant the container.</p>
 
<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg" style="width: 600px; height: 287px;" />
<pre>
<strong>Input:</strong> height = [1,8,6,2,5,4,8,3,7]
<strong>Output:</strong> 49
<strong>Explanation:</strong> The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
</pre>
 
<p><strong class="example">Example 2:</strong></p>
 
<pre>
<strong>Input:</strong> height = [1,1]
<strong>Output:</strong> 1
</pre>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li><code>n == height.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= height[i] &lt;= 10<sup>4</sup></code></li>
</ul>
 
 
 
> [!tip]- Hint 1
> 
> If you simulate the problem, it will be O(n^2) which is not efficient.
 
> [!tip]- Hint 2
> 
> Try to use two-pointers. Set one pointer to the left and one to the right of the array. Always move the pointer that points to the lower line.
 
> [!tip]- Hint 3
> 
> How can you calculate the amount of water at each step?
 
 
---
 
[submissions](https://leetcode.com/problems/container-with-most-water/submissions/) | [solutions](https://leetcode.com/problems/container-with-most-water/solutions/)
 
 
tab: 中文
 
<p>给定一个长度为 <code>n</code> 的整数数组&nbsp;<code>height</code>&nbsp;。有&nbsp;<code>n</code>&nbsp;条垂线,第 <code>i</code> 条线的两个端点是&nbsp;<code>(i, 0)</code>&nbsp;和&nbsp;<code>(i, height[i])</code>&nbsp;。</p>
 
<p>找出其中的两条线,使得它们与&nbsp;<code>x</code>&nbsp;轴共同构成的容器可以容纳最多的水。</p>
 
<p>返回容器可以储存的最大水量。</p>
 
<p><strong>说明:</strong>你不能倾斜容器。</p>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
 
<p><img alt="" src="https://aliyun-lc-upload.oss-cn-hangzhou.aliyuncs.com/aliyun-lc-upload/uploads/2018/07/25/question_11.jpg" /></p>
 
<pre>
<strong>输入:</strong>[1,8,6,2,5,4,8,3,7]
<strong>输出:</strong>49 
<strong>解释:</strong>图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为&nbsp;49。</pre>
 
<p><strong>示例 2:</strong></p>
 
<pre>
<strong>输入:</strong>height = [1,1]
<strong>输出:</strong>1
</pre>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li><code>n == height.length</code></li>
	<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= height[i] &lt;= 10<sup>4</sup></code></li>
</ul>
 
 
 
> [!tip]- 提示 1
> 
> If you simulate the problem, it will be O(n^2) which is not efficient.
 
> [!tip]- 提示 2
> 
> Try to use two-pointers. Set one pointer to the left and one to the right of the array. Always move the pointer that points to the lower line.
 
> [!tip]- 提示 3
> 
> How can you calculate the amount of water at each step?
 
 
---
 
[提交记录](https://leetcode.cn/problems/container-with-most-water/submissions/) | [题解](https://leetcode.cn/problems/container-with-most-water/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122