> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/11.-container-with-most-water.md).

# 11. Container With Most Water

## 1.問題

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LLZDqxQCu_mTVtU_3xd%2F-LLZEvV6PcTSpdPyYuJE%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-09-04%20%E4%B8%8B%E5%8D%887.02.26.png?alt=media\&token=a8c0f62a-7541-483d-b654-ecaba464d848)

## 2.想法

* 提問
* function header, parameter
* test input
  * zero vector
* 說明想法
  * 暴力解: 雙重迴圈
  * 優化解
    * 其實面積是左右兩邊, 可以設定為比較左右兩邊的長度, 誰比較短則往前進一格
* 測試計算複雜度: O(n)?

## **3.程式碼**

```
class Solution {
public:
    int maxArea(vector<int>& height) {
        int size = height.size() - 1, r = 0, l = size, maxSize = min(height[r], height[l]) * size;
        
        while (r < l) {
            if (height[r] < height[l]) {
                r++;
            } else {
                l--;
            }
            
            int currSize = min(height[r], height[l]) * (l - r);
            maxSize = max(maxSize, currSize);
        }
        
        
        return maxSize;
    }
};



```
