diff --git a/LeetCode/Container_with_Most_Water/PROBLEM.md b/LeetCode/Container_with_Most_Water/PROBLEM.md new file mode 100644 index 0000000..53589b7 --- /dev/null +++ b/LeetCode/Container_with_Most_Water/PROBLEM.md @@ -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 diff --git a/LeetCode/Container_with_Most_Water/Python/README.md b/LeetCode/Container_with_Most_Water/Python/README.md new file mode 100644 index 0000000..75ac83a --- /dev/null +++ b/LeetCode/Container_with_Most_Water/Python/README.md @@ -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) diff --git a/LeetCode/Container_with_Most_Water/Python/Solution.py b/LeetCode/Container_with_Most_Water/Python/Solution.py new file mode 100644 index 0000000..3bcbc20 --- /dev/null +++ b/LeetCode/Container_with_Most_Water/Python/Solution.py @@ -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 diff --git a/LeetCode/Container_with_Most_Water/Python/snapshot.png b/LeetCode/Container_with_Most_Water/Python/snapshot.png new file mode 100644 index 0000000..1424341 Binary files /dev/null and b/LeetCode/Container_with_Most_Water/Python/snapshot.png differ diff --git a/LeetCode/Container_with_Most_Water/example-1.png b/LeetCode/Container_with_Most_Water/example-1.png new file mode 100644 index 0000000..7e422df Binary files /dev/null and b/LeetCode/Container_with_Most_Water/example-1.png differ