-
Notifications
You must be signed in to change notification settings - Fork 0
feat: solve Container With Most Water (#11) #226
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5f1c552
test: cycle 1 (Green)
pertrai1 dbf046a
test: cycle 2 (Green)
pertrai1 228c194
test: cycle 3 (Green)
pertrai1 cf6725f
test: cycle 4 (Green)
pertrai1 c78299f
test: cycle 5 (Green)
pertrai1 9b627ea
feat: solve Container With Most Water (#11) via TDD
pertrai1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
162 changes: 162 additions & 0 deletions
162
leetcode/medium/0011-container-with-most-water/POST_MORTEM.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,162 @@ | ||
| # Post-Mortem Log | ||
|
|
||
| ## Problem | ||
|
|
||
| Given an integer array `height` representing n vertical lines, find two lines that together with the x-axis form a container that holds the most water. Return the maximum water area. | ||
|
|
||
| - **Problem Name:** Container With Most Water | ||
| - **Problem Link:** [LeetCode #11](https://leetcode.com/problems/container-with-most-water/) | ||
| - **Date:** 2026-02-07 | ||
|
|
||
| --- | ||
|
|
||
| ## Time Tracking | ||
|
|
||
| - **Time to design the algorithm:** ~5 minutes | ||
| - **Time to code:** ~10 minutes (5 TDD cycles) | ||
| - **Time debugging/fixing:** 0 minutes (TDD approach prevented bugs) | ||
|
|
||
| --- | ||
|
|
||
| ## Solution Exploration | ||
|
|
||
| ### What approaches did I consider? | ||
|
|
||
| 1. **Brute Force (O(nΒ²))**: Check every pair of lines | ||
| - Time: O(nΒ²), Space: O(1) | ||
| - Nested loops to compare all possible containers | ||
| - Works but too slow for n β€ 10β΅ | ||
|
|
||
| 2. **Two Pointers (Optimal - O(n))**: Start wide, move inward strategically | ||
| - Time: O(n), Space: O(1) | ||
| - Start with widest container (left=0, right=n-1) | ||
| - Move the pointer at the shorter height inward | ||
| - This is the optimal solution | ||
|
|
||
| ### Final Solution Analysis | ||
|
|
||
| - **Time Complexity:** O(n) - single pass through the array | ||
| - **Space Complexity:** O(1) - only constant extra space | ||
| - **Is it optimal?** Yes - we must examine at least O(n) elements to find the maximum, and we do exactly that in one pass | ||
|
|
||
| ### Why this approach worked (or didn't) | ||
|
|
||
| The two-pointer approach works because of a key greedy insight: when we have a container formed by left and right pointers, the area is limited by the shorter height. Moving the taller height inward can only decrease the area (width decreases, height stays limited by the shorter side). However, moving the shorter height inward gives us a chance to find a taller line that could increase the area despite the reduced width. | ||
|
|
||
| --- | ||
|
|
||
| ## Pattern Recognition | ||
|
|
||
| ### What algorithmic pattern does this problem use? | ||
|
|
||
| - **Two Pointers (Opposite Ends)** | ||
| - **Greedy Algorithm** | ||
|
|
||
| ### Key Insight | ||
|
|
||
| The "aha moment" is recognizing that we should **always move the pointer at the shorter height** inward. This is because: | ||
|
|
||
| 1. The container's height is limited by `min(height[left], height[right])` | ||
| 2. The width is `right - left` | ||
| 3. Moving inward always decreases width | ||
| 4. Moving the taller pointer cannot increase the min height (still limited by shorter side) | ||
| 5. Moving the shorter pointer might find a taller line, potentially increasing area | ||
|
|
||
| This greedy choice is safe because we can prove we won't miss the optimal solution. | ||
|
|
||
| ### Related Problems | ||
|
|
||
| - [42. Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/) - Similar two-pointer approach with height constraints | ||
| - [15. 3Sum](https://leetcode.com/problems/3sum/) - Two pointers after sorting | ||
| - [167. Two Sum II - Input Array Is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) - Classic two-pointer pattern | ||
|
|
||
| --- | ||
|
|
||
| ## Edge Cases & Verification | ||
|
|
||
| ### What clarifying questions did I ask? | ||
|
|
||
| - Can heights be zero? (Yes, based on constraints) | ||
| - What's the minimum array length? (2, as stated in constraints) | ||
| - Can we slant the container? (No, area is always a rectangle) | ||
|
|
||
| ### Edge cases handled | ||
|
|
||
| - Minimum length array (2 elements) | ||
| - All same heights | ||
| - Strictly increasing heights | ||
| - Heights with zeros | ||
| - Large arrays (up to 10β΅ elements) | ||
|
|
||
| ### Edge cases missed | ||
|
|
||
| None - the TDD approach helped us systematically cover edge cases. | ||
|
|
||
| --- | ||
|
|
||
| ## Mistakes & Bugs | ||
|
|
||
| ### Mistakes I keep making | ||
|
|
||
| - None in this session - TDD helped prevent common mistakes like: | ||
| - Off-by-one errors in pointer movement | ||
| - Incorrect area calculation | ||
| - Missing edge cases | ||
|
|
||
| ### Bugs to add to the Bug List | ||
|
|
||
| - N/A - Clean implementation on first try thanks to TDD | ||
|
|
||
| --- | ||
|
|
||
| ## Retrospective | ||
|
|
||
| ### Key Takeaways & Lessons Learned | ||
|
|
||
| 1. **TDD is powerful for algorithmic problems**: Writing tests first helped clarify the problem requirements and catch edge cases early | ||
| 2. **Greedy algorithms need proof**: The key insight (move shorter pointer) needed logical reasoning to verify correctness | ||
| 3. **Two-pointer patterns are efficient**: When searching for pairs/subarrays, consider if two pointers can reduce O(nΒ²) to O(n) | ||
| 4. **Start simple, then optimize**: First test was the simplest case, which helped verify the core algorithm logic | ||
|
|
||
| ### Add to Cheat Sheet | ||
|
|
||
| **Two Pointers (Opposite Ends) Template:** | ||
|
|
||
| ```typescript | ||
| function twoPointerOppositeEnds(arr: number[]): number { | ||
| let left = 0; | ||
| let right = arr.length - 1; | ||
| let result = 0; // or other initial value | ||
|
|
||
| while (left < right) { | ||
| // Calculate current value | ||
| const current = /* some calculation */; | ||
| result = Math.max(result, current); | ||
|
|
||
| // Move pointers based on problem-specific logic | ||
| if (/* condition */) { | ||
| left++; | ||
| } else { | ||
| right--; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| ``` | ||
|
|
||
| **Container With Most Water Formula:** | ||
| `area = min(height[left], height[right]) Γ (right - left)` | ||
|
|
||
| --- | ||
|
|
||
| ## Rubric Self-Rating (1β5) | ||
|
|
||
| | Category | Rating | Notes | | ||
| | ------------------- | ------ | --------------------------------------------------------------------- | | ||
| | **Problem solving** | 5 | Identified optimal O(n) solution immediately, understood greedy logic | | ||
| | **Coding** | 5 | Clean implementation, no bugs, good variable names | | ||
| | **Verification** | 5 | TDD approach ensured comprehensive test coverage and edge cases | | ||
| | **Communication** | 5 | Clear understanding of algorithm and ability to explain the logic | | ||
|
|
||
| --- |
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,39 @@ | ||
| # 11. Container With Most Water | ||
|
|
||
| **Difficulty:**  | ||
|
|
||
| **Link:** [Container With Most Water - LeetCode](https://leetcode.com/problems/container-with-most-water/) | ||
|
|
||
| ## Problem Description | ||
|
|
||
| Given an integer array `height` of length `n` where `n` vertical lines are drawn such that the two endpoints of the `i`th line are `(i, 0)` and `(i, height[i])`. | ||
|
|
||
| Find two lines that together with the x-axis form a container such that the container holds the most water. | ||
|
|
||
| Return the maximum amount of water a container can store. | ||
|
|
||
| **Note:** You may not slant the container. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Example 1 | ||
|
|
||
| ```text | ||
| Input: height = [1,8,6,2,5,4,8,3,7] | ||
| Output: 49 | ||
| ``` | ||
|
|
||
| **Explanation:** The vertical lines at indices 1 (height = 8) and 8 (height = 7) form a container with area = min(8,7) Γ (8-1) = 7 Γ 7 = 49 | ||
|
|
||
| ### Example 2 | ||
|
|
||
| ```text | ||
| Input: height = [1,1] | ||
| Output: 1 | ||
| ``` | ||
|
|
||
| ## Constraints | ||
|
|
||
| - `n == height.length` | ||
| - `2 <= n <= 10^5` | ||
| - `0 <= height[i] <= 10^4` |
27 changes: 27 additions & 0 deletions
27
leetcode/medium/0011-container-with-most-water/container-with-most-water.test.ts
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,27 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { maxArea } from './container-with-most-water'; | ||
|
|
||
| describe('Container With Most Water', () => { | ||
| it('should return 1 for height [1,1]', () => { | ||
| expect(maxArea([1, 1])).toBe(1); | ||
| }); | ||
|
|
||
| it('should return 49 for height [1,8,6,2,5,4,8,3,7]', () => { | ||
| expect(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])).toBe(49); | ||
| }); | ||
|
|
||
| it('should handle all same heights [3,3,3,3]', () => { | ||
| // All heights are 3, widest container is 0 to 3 = width 3, height 3 = area 9 | ||
| expect(maxArea([3, 3, 3, 3])).toBe(9); | ||
| }); | ||
|
|
||
| it('should handle increasing heights [1,2,3,4,5]', () => { | ||
| // Best container is 0 to 4: min(1,5) Γ 4 = 1 Γ 4 = 4 | ||
| expect(maxArea([1, 2, 3, 4, 5])).toBe(6); | ||
| }); | ||
|
|
||
| it('should handle heights with zeros [0,2,0,3,0]', () => { | ||
| // Best container is 1 to 3: min(2,3) Γ 2 = 2 Γ 2 = 4 | ||
| expect(maxArea([0, 2, 0, 3, 0])).toBe(4); | ||
| }); | ||
| }); | ||
21 changes: 21 additions & 0 deletions
21
leetcode/medium/0011-container-with-most-water/container-with-most-water.ts
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,21 @@ | ||
| export function maxArea(height: number[]): number { | ||
| let maxArea = 0; | ||
| let left = 0; | ||
| let right = height.length - 1; | ||
|
|
||
| while (left < right) { | ||
| const width = right - left; | ||
| const currentHeight = Math.min(height[left], height[right]); | ||
| const area = width * currentHeight; | ||
| maxArea = Math.max(maxArea, area); | ||
|
|
||
| // Move the pointer with smaller height | ||
| if (height[left] < height[right]) { | ||
| left++; | ||
| } else { | ||
| right--; | ||
| } | ||
| } | ||
|
|
||
| return maxArea; | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Misleading comment β the best container is not at indices 0 and 4.
The expected result of
6is correct, but the comment claims it comes frommin(1,5) Γ 4 = 4. The actual best pair is at indices 1 and 4:min(2,5) Γ 3 = 6(or equivalently indices 2 and 4:min(3,5) Γ 2 = 6).π Proposed fix
it('should handle increasing heights [1,2,3,4,5]', () => { - // Best container is 0 to 4: min(1,5) Γ 4 = 1 Γ 4 = 4 + // Best container is indices 1 to 4: min(2,5) Γ 3 = 2 Γ 3 = 6 expect(maxArea([1, 2, 3, 4, 5])).toBe(6); });π Committable suggestion
π€ Prompt for AI Agents