Skip to content

Commit d269b70

Browse files
committed
Add: Exercise 11. Container With Most Water
1 parent 6368807 commit d269b70

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 11. Container With Most Water
2+
You are given an integer array `height` of length `n`. There are `n` vertical lines drawn such that
3+
the two endpoints of the ith line are `(i, 0)` and `(i, height[i])`.
4+
5+
Find two lines that together with the x-axis form a container, such that the container contains the
6+
most water.
7+
8+
Return the maximum amount of water a container can store.
9+
10+
Notice that you may not slant the container.
11+
12+
## Examples
13+
14+
### Example 1:
15+
![Example 1](./img1.jpg)
16+
17+
**Input:** `height` = [1,8,6,2,5,4,8,3,7]
18+
19+
**Output:** 49
20+
21+
**Explanation:** The above vertical lines are represented by array `[1,8,6,2,5,4,8,3,7]`. In this
22+
case, the max area of water (blue section) the container can contain is 49.
23+
24+
### Example 2:
25+
**Input:** `height` = [1,1]
26+
27+
**Output:** 1
28+
29+
## Constraints
30+
- `n == height.length`
31+
- `2 <= n <= 10^5`
32+
- `0 <= height[i] <= 10^4`

11-container-with-most-water/img1.jpg

17.9 KB
Loading

11-container-with-most-water/index.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Determines the maximum water volume between pairs of lines in `height`.
3+
* Implements a two-pointer approach, moving from the edges to the center.
4+
* The water volume is calculated as the min height of the two lines times the width.
5+
* @param {number[]} height - Heights of the lines.
6+
* @return {number} - Maximum water volume that can be contained.
7+
*/
8+
var maxArea = function(height) {
9+
const n = height.length
10+
11+
// We initialize the container limits with two pointers
12+
let left = 0 // Index of the left limit (pointer) of the container
13+
let right = n - 1 // Index of the right limit of the container
14+
let maxArea = 0
15+
16+
while (left < right) {
17+
const base = right - left
18+
const area = Math.min(height[left], height[right]) * base
19+
20+
maxArea = Math.max(area, maxArea)
21+
22+
// This is the most critical part, which pointer to move?
23+
// The smaller height limits the max area of the container, so we shift the
24+
// pointer with the smaller height to the left or right, searching for a bigger
25+
// height
26+
if (height[left] < height[right]) {
27+
left++
28+
} else {
29+
right--
30+
}
31+
}
32+
33+
return maxArea
34+
};
35+
36+
module.exports = maxArea
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "11-container-with-most-water",
3+
"version": "1.0.0",
4+
"description": "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])`.",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"debug": "node --inspect-brk index.js",
9+
"test": "node test.js"
10+
},
11+
"keywords": [],
12+
"author": "",
13+
"license": "ISC"
14+
}

11-container-with-most-water/test.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const assert = require('assert')
2+
const maxArea = require('./index')
3+
4+
const tt = [
5+
{
6+
height: [1,8,6,2,5,4,8,3,7],
7+
expected: 49
8+
},
9+
{
10+
height: [1,1],
11+
expected: 1
12+
},
13+
]
14+
15+
for (const t of tt) {
16+
assert.strict.deepEqual(maxArea(t.height), t.expected)
17+
}

0 commit comments

Comments
 (0)