|
| 1 | +# Container With Most Water (Medium) |
| 2 | + |
| 3 | +**Problem ID:** 11 |
| 4 | +**Date:** 2025-10-04 |
| 5 | +**Link:** https://leetcode.com/problems/container-with-most-water/ |
| 6 | + |
| 7 | +## Approach |
| 8 | + |
| 9 | +To solve the "Container With Most Water" problem, we can utilize a two-pointer approach, which is efficient and optimal for this scenario. |
| 10 | + |
| 11 | +### Problem Understanding: |
| 12 | +The goal is to find two vertical lines from the given array that, together with the x-axis, form a container that can hold the maximum amount of water. The water held by the container is determined by the shorter of the two lines and the distance between them. |
| 13 | + |
| 14 | +### Approach: |
| 15 | +1. **Two-Pointer Technique**: |
| 16 | + - Initialize two pointers: `left` at the beginning (index 0) and `right` at the end (index n-1) of the array. |
| 17 | + - Calculate the area formed by the lines at these two pointers. The area can be computed using the formula: |
| 18 | + \[ |
| 19 | + \text{Area} = \text{min}(height[left], height[right]) \times (right - left) |
| 20 | + \] |
| 21 | + - Keep track of the maximum area encountered. |
| 22 | + |
| 23 | +2. **Pointer Adjustment**: |
| 24 | + - To potentially find a larger area, we need to adjust the pointers: |
| 25 | + - If the height at the `left` pointer is less than the height at the `right` pointer, increment the `left` pointer (move it to the right). |
| 26 | + - Otherwise, decrement the `right` pointer (move it to the left). |
| 27 | + - This adjustment is based on the fact that moving the pointer pointing to the shorter line might lead to a taller line, which could increase the area. |
| 28 | + |
| 29 | +3. **Iteration**: |
| 30 | + - Continue this process until the two pointers meet. During each iteration, update the maximum area if the current area is greater than the previously recorded maximum. |
| 31 | + |
| 32 | +### Data Structures: |
| 33 | +- The primary data structure used is the integer array `height`, which stores the heights of the vertical lines. |
| 34 | +- Two integer variables are used for the pointers (`left` and `right`) and one for tracking the maximum area. |
| 35 | + |
| 36 | +### Complexity: |
| 37 | +- **Time Complexity**: O(n), where n is the length of the `height` array. Each element is processed at most twice (once by each pointer). |
| 38 | +- **Space Complexity**: O(1), as we are using a constant amount of extra space regardless of the input size. |
| 39 | + |
| 40 | +This two-pointer approach efficiently narrows down the potential maximum area without needing to check every possible pair of lines, making it suitable for the constraints provided in the problem. |
0 commit comments