Skip to content

Commit a7909d4

Browse files
committed
Add solution for #11 Container With Most Water.
1 parent ffacd5e commit a7909d4

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Container With Most Water
2+
3+
LeetCode #: [11](https://leetcode.com/problems/container-with-most-water/)
4+
5+
Difficulty: Medium
6+
7+
Topics: Array, Two Pointers.
8+
9+
## Problem
10+
11+
Given `n` non-negative integers `a1, a2, ..., an` , where each represents a point at coordinate `(i, ai)`. `n` vertical lines are drawn such that the two endpoints of the line `i` is at `(i, ai)` and `(i, 0)`. Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
12+
13+
Notice that you may not slant the container.
14+
15+
Example 1:
16+
17+
![Example 1](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
18+
19+
```text
20+
Input: height = [1,8,6,2,5,4,8,3,7]
21+
Output: 49
22+
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.
23+
```
24+
25+
Example 2:
26+
27+
```text
28+
Input: height = [1,1]
29+
Output: 1
30+
```
31+
32+
Example 3:
33+
34+
```text
35+
Input: height = [4,3,2,1,4]
36+
Output: 16
37+
```
38+
39+
Example 4:
40+
41+
```text
42+
Input: height = [1,2,1]
43+
Output: 2
44+
```
45+
46+
Constraints:
47+
48+
- `n == height.length`
49+
- `2 <= n <= 3 * 10^4`
50+
- `0 <= height[i] <= 3 * 10^4`
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} height
3+
* @return {number}
4+
*/
5+
const maxArea = (height) => {
6+
let left = 0
7+
let right = height.length - 1
8+
let area = 0
9+
10+
while (left < right) {
11+
const w = right - left
12+
const h = Math.min(height[left], height[right])
13+
area = Math.max(area, w * h)
14+
15+
if (height[left] < height[right]) {
16+
left++
17+
} else {
18+
right--
19+
}
20+
}
21+
22+
return area
23+
}
24+
25+
module.exports = maxArea
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const maxArea = require('./maxArea')
2+
3+
test('Example 1', () => {
4+
const height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
5+
6+
const result = maxArea(height)
7+
8+
expect(result).toStrictEqual(49)
9+
})
10+
11+
test('Example 2', () => {
12+
const height = [1, 1]
13+
14+
const result = maxArea(height)
15+
16+
expect(result).toStrictEqual(1)
17+
})
18+
19+
test('Example 3', () => {
20+
const height = [4, 3, 2, 1, 4]
21+
22+
const result = maxArea(height)
23+
24+
expect(result).toStrictEqual(16)
25+
})
26+
27+
test('Example 4', () => {
28+
const height = [1, 2, 1]
29+
30+
const result = maxArea(height)
31+
32+
expect(result).toStrictEqual(2)
33+
})

0 commit comments

Comments
 (0)