# LC 11. Container With Most Water ### [Problem link](https://leetcode.com/problems/container-with-most-water/) ###### tags: `leedcode` `medium` `c++` `Two Pointer` 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>. Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. **Notice** that you may not slant the container. **Example 1:** <img alt="" src="https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg" style="width: 600px; height: 287px;" /> ``` Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: 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. ``` **Example 2:** ``` Input: height = [1,1] Output: 1 ``` **Constraints:** - <code>n == height.length</code> - <code>2 <= n <= 10<sup>5</sup></code> - <code>0 <= height[i] <= 10<sup>4</sup></code> ## Solution 1 - Two Pointer #### C++ ```cpp= class Solution { public: int maxArea(vector<int>& height) { int left = 0; int right = height.size() - 1; int ans = 0; while (left < right) { int area = (right - left) * min(height[left], height[right]); ans = max(ans, area); if (height[left] < height[right]) { left++; } else { right--; } } return ans; } }; ``` >### Complexity >| | Time Complexity | Space Complexity | >| ----------- | --------------- | ---------------- | >| Solution 1 | O(n) | O(1) | ## Note [ref](https://www.bilibili.com/video/BV1Qg411q7ia/?spm_id_from=333.788&vd_source=088937f16fb413336c0cb260ed86a1c3)