Skip to content

Commit 621d23e

Browse files
chore: add LeetCode daily solution
1 parent a99fc8d commit 621d23e

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Container With Most Water (Medium)
2+
3+
**Problem ID:** 11
4+
**Date:** 2025-10-04
5+
**Link:** https://leetcode.com/problems/container-with-most-water/
6+
7+
## Approach
8+
9+
To solve the "Container With Most Water" problem, we can utilize a two-pointer approach, which is efficient and optimal for this scenario.
10+
11+
### Problem Understanding:
12+
The goal is to find two vertical lines from the given array that, together with the x-axis, form a container that can hold the maximum amount of water. The water held by the container is determined by the shorter of the two lines and the distance between them.
13+
14+
### Approach:
15+
1. **Two-Pointer Technique**:
16+
- Initialize two pointers: `left` at the beginning (index 0) and `right` at the end (index n-1) of the array.
17+
- Calculate the area formed by the lines at these two pointers. The area can be computed using the formula:
18+
\[
19+
\text{Area} = \text{min}(height[left], height[right]) \times (right - left)
20+
\]
21+
- Keep track of the maximum area encountered.
22+
23+
2. **Pointer Adjustment**:
24+
- To potentially find a larger area, we need to adjust the pointers:
25+
- If the height at the `left` pointer is less than the height at the `right` pointer, increment the `left` pointer (move it to the right).
26+
- Otherwise, decrement the `right` pointer (move it to the left).
27+
- This adjustment is based on the fact that moving the pointer pointing to the shorter line might lead to a taller line, which could increase the area.
28+
29+
3. **Iteration**:
30+
- Continue this process until the two pointers meet. During each iteration, update the maximum area if the current area is greater than the previously recorded maximum.
31+
32+
### Data Structures:
33+
- The primary data structure used is the integer array `height`, which stores the heights of the vertical lines.
34+
- Two integer variables are used for the pointers (`left` and `right`) and one for tracking the maximum area.
35+
36+
### Complexity:
37+
- **Time Complexity**: O(n), where n is the length of the `height` array. Each element is processed at most twice (once by each pointer).
38+
- **Space Complexity**: O(1), as we are using a constant amount of extra space regardless of the input size.
39+
40+
This two-pointer approach efficiently narrows down the potential maximum area without needing to check every possible pair of lines, making it suitable for the constraints provided in the problem.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int maxArea(int[] height) {
3+
int left = 0, right = height.length - 1;
4+
int maxArea = 0;
5+
6+
while (left < right) {
7+
int width = right - left;
8+
int currentHeight = Math.min(height[left], height[right]);
9+
maxArea = Math.max(maxArea, width * currentHeight);
10+
11+
if (height[left] < height[right]) {
12+
left++;
13+
} else {
14+
right--;
15+
}
16+
}
17+
18+
return maxArea;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var maxArea = function(height) {
2+
let left = 0;
3+
let right = height.length - 1;
4+
let maxArea = 0;
5+
6+
while (left < right) {
7+
const width = right - left;
8+
const currentHeight = Math.min(height[left], height[right]);
9+
const area = width * currentHeight;
10+
maxArea = Math.max(maxArea, area);
11+
12+
if (height[left] < height[right]) {
13+
left++;
14+
} else {
15+
right--;
16+
}
17+
}
18+
19+
return maxArea;
20+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
left, right = 0, len(height) - 1
4+
max_area = 0
5+
6+
while left < right:
7+
width = right - left
8+
current_height = min(height[left], height[right])
9+
max_area = max(max_area, width * current_height)
10+
11+
if height[left] < height[right]:
12+
left += 1
13+
else:
14+
right -= 1
15+
16+
return max_area

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,4 @@ Each problem includes:
166166
- 2025-10-01 — [Water Bottles](https://leetcode.com/problems/water-bottles/) (Easy) → `Easy/2025-10-01-1518-Water-Bottles`
167167
- 2025-10-02 — [Water Bottles II](https://leetcode.com/problems/water-bottles-ii/) (Medium) → `Medium/2025-10-02-3100-Water-Bottles-II`
168168
- 2025-10-03 — [Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) (Hard) → `Hard/2025-10-03-407-Trapping-Rain-Water-II`
169+
- 2025-10-04 — [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) (Medium) → `Medium/2025-10-04-11-Container-With-Most-Water`

0 commit comments

Comments
 (0)