Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved the challenge '11. Container with the most water' on Leetcode #159

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions LeetCode/Container_with_Most_Water/PROBLEM.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# [11. Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/)

Given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

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:**

![example-1](/LeetCode/Container_with_Most_Water/example-1.png)
```example 1
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:**
- n == height.length
- 2 <= n <= 105
- 0 <= height[i] <= 104
18 changes: 18 additions & 0 deletions LeetCode/Container_with_Most_Water/Python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Container With Most Water
This problem can be solved using the Greedy algorithm. The idea behind the Greedy algorithm is to make locally optimal decisions at each step in order to achieve a globally optimal solution. In this case, we can use two pointers (left pointer and right pointer) to select the two lines that form the container.

## Algorithm
1. Initialize the left pointer to the beginning of the array and the right pointer to the end of the array.
2. Compute the area of the container formed by these two lines by taking the difference between the x position of the two pointers (i.e., the distance between the two lines) and multiplying this difference by the minimum height of the two lines.
3. Compare the current area with the maximum area encountered so far and update the maximum area if necessary.
4. Move the left pointer to the right if the height at this position is less than the height at the position of the right pointer, otherwise move the right pointer to the left.
5. Repeat steps 2-4 until the two pointers cross each other.
6. At this point, we have found the maximum possible area for the container and we return it.

## Complexity Analysis

- **Time complexity:** The time complexity of this algorithm is **O(n)** because we iterate through the elements of the array only once using the two pointers.

- **Space Complexity:** The space complexity is O(1) because we only use additional variables to store the positions of the pointers.

![Implementation](/LeetCode/Container_with_Most_Water/Python/Solution.py)
15 changes: 15 additions & 0 deletions LeetCode/Container_with_Most_Water/Python/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = 0
left = 0
right = len(height) - 1

while left < right:
current_area = min(height[left], height[right]) * (right - left)
max_area = max(max_area, current_area)

if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added LeetCode/Container_with_Most_Water/example-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading