11. Container With Most Water
Tags:
Medium
Skills:
Two pointer
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Bài toàn yêu cầu tình hai đường thẳng (từ mảng height ) tạo thành một container chứa được nhiều nước nhất (diện tích lớn nhất giữa hai đường thảng và trục hoành)
Approach
Sử dụng two pointers
area = min(height[left], height[right] * (right - left)Solution
1function maxArea(height: number[]): number {
2 let left = 0;
3 let right = height.length - 1;
4 let max_area = 0;
5
6 while (left < right) {
7 const min_height = Math.min(height[left], height[right]);
8 const width = right - left;
9 const area = min_height * width;
10 max_area = Math.max(max_area, area);
11
12 if (height[left] < height[right]) {
13 left++
14 } else {
15 right--
16 }
17 }
18 return max_area;
19};