From 9cbcc547a6928ff12944ccdbb8aee0929d459e61 Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Sun, 27 Oct 2024 17:13:20 +0300 Subject: [PATCH 1/3] feat: add container with most water --- .../container_with_most_water/__init__.py | 0 .../container_with_most_water.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 arrays_and_strings/container_with_most_water/__init__.py create mode 100644 arrays_and_strings/container_with_most_water/container_with_most_water.py 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..0fbbcc5 --- /dev/null +++ b/arrays_and_strings/container_with_most_water/container_with_most_water.py @@ -0,0 +1,19 @@ +def max_area(height: list[int]) -> int: + left = 0 + right = len(height) - 1 + max_area = 0 + + while left < right: + curr_height = min(height[left], height[right]) + curr_width = right - left + curr_area = curr_height * curr_width + + max_area = max(curr_area, max_area) + + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return max_area + \ No newline at end of file From 565b22b42a2e6785b238aa69605116c51754707e Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Sun, 27 Oct 2024 17:19:08 +0300 Subject: [PATCH 2/3] doc: document algorithm --- .../container_with_most_water.py | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) 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 index 0fbbcc5..7b67bf8 100644 --- 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 @@ -1,19 +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 - \ No newline at end of file + +# 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 From 68e223b9e48aa6256df1b61efb3cc5f30f84e80f Mon Sep 17 00:00:00 2001 From: AgathaBahati Date: Sun, 27 Oct 2024 17:26:27 +0300 Subject: [PATCH 3/3] doc: add README --- .../container_with_most_water/README.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 arrays_and_strings/container_with_most_water/README.md 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy