diff --git a/arrays_and_strings/container_with_most_water/README.md b/arrays_and_strings/container_with_most_water/README.md new file mode 100644 index 0000000..7ee6c9c --- /dev/null +++ b/arrays_and_strings/container_with_most_water/README.md @@ -0,0 +1,23 @@ +## **Problem Statement** + +You are 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 + Input: height = [1,8,6,2,5,4,8,3,7] + Output: 49 + + Explanation: The vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. + In this case, the max area of water the container can contain is 49. Derived from (1, height[1]) and (8, height[8]) which gives us the heights; h1 = 8, h2 = 7. + To get the width: 8 - 1 = 7 + To get height = min(8, 7) = 7 + To get area = height * width = 7 * 7 = 49 + +### Example 2 + Input: height = [1,1] + Output: 1 \ No newline at end of file diff --git a/arrays_and_strings/container_with_most_water/__init__.py b/arrays_and_strings/container_with_most_water/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/arrays_and_strings/container_with_most_water/container_with_most_water.py b/arrays_and_strings/container_with_most_water/container_with_most_water.py new file mode 100644 index 0000000..7b67bf8 --- /dev/null +++ b/arrays_and_strings/container_with_most_water/container_with_most_water.py @@ -0,0 +1,54 @@ +def max_area(height: list[int]) -> int: + """ + Calculate the maximum area of water a container can store, formed by two lines + from the given list of heights and the x-axis. + + Parameters: + height (list[int]): A list of integers representing the heights of vertical lines. + + Returns: + int: The maximum area of water that can be contained. + + """ + left = 0 + right = len(height) - 1 + max_area = 0 + + while left < right: + # Calculate the height and width of the current container + curr_height = min(height[left], height[right]) + curr_width = right - left + curr_area = curr_height * curr_width + + # Update max_area if the current area is larger + max_area = max(curr_area, max_area) + + # Move the pointer pointing to the shorter line + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return max_area + +# Approach and Reasoning: +# ----------------------- +# - We use the two-pointer technique to solve this problem efficiently. +# - Initialize two pointers, `left` at the start and `right` at the end of the list. +# - The width of the container is the distance between the two pointers. +# - The height of the container is determined by the shorter of the two lines at the pointers. +# - Calculate the area for the current pair of lines and update `max_area` if this area is larger. +# - Move the pointer pointing to the shorter line inward to potentially find a taller line, +# which might result in a larger area. +# - Repeat the process until the two pointers meet. + +# Time Complexity: +# ---------------- +# - The time complexity of this approach is O(n), where n is the number of elements in the `height` list. +# - This is because each element is processed at most once as the pointers move towards each other. + +# Space Complexity: +# ----------------- +# - The space complexity is O(1) because we are using a constant amount of extra space, +# regardless of the input size. + \ No newline at end of file
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: