- Notifications
You must be signed in to change notification settings - Fork12
feat: container with most water#363
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
23 changes: 23 additions & 0 deletionsarrays_and_strings/container_with_most_water/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Empty file.
54 changes: 54 additions & 0 deletionsarrays_and_strings/container_with_most_water/container_with_most_water.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.