Skip to content

Commit d06ba69

Browse files
Added Container With Most Water
1 parent 6a6b6b3 commit d06ba69

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Container With Most Water
3+
https://leetcode.com/problems/container-with-most-water/
4+
5+
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]).
6+
7+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
8+
9+
Return the maximum amount of water a container can store.
10+
11+
Notice that you may not slant the container.
12+
13+
Example 1:
14+
Input: height = [1,8,6,2,5,4,8,3,7]
15+
Output: 49
16+
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.
17+
Example 2:
18+
19+
Input: height = [1,1]
20+
Output: 1
21+
*/
22+
23+
/**
24+
* @param {number[]} height
25+
* @return {number}
26+
*/
27+
var maxArea = function(height) {
28+
let left = 0;
29+
let right = height.length - 1;
30+
let maxArea = calculateArea(left, right, height);
31+
32+
while(left < right) {
33+
if(height[left] < height[right]) {
34+
left++
35+
} else {
36+
right--;
37+
}
38+
maxArea = Math.max(maxArea, calculateArea(left, right, height))
39+
}
40+
return maxArea;
41+
};
42+
43+
var calculateArea = function(x, y, height) {
44+
let minHeight = height[x] > height[y] ? height[y] : height[x];
45+
let width = y -x;
46+
return (width * minHeight);
47+
}
48+
49+
module.exports.maxArea = maxArea;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const assert = require('assert');
2+
const maxArea = require('../../LeetcodeProblems/Algorithms/Container_With_Most_Water_Test').maxArea;
3+
4+
function test() {
5+
assert.equal(49, maxArea([1,8,6,2,5,4,8,3,7]));
6+
assert.equal(1, maxArea([1,1]));
7+
8+
}
9+
10+
module.exports.test = test;

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
2929
| [Add Two Numbers ](/LeetcodeProblems/Algorithms/Add_Two_Numbers.js) | Medium | https://leetcode.com/problems/add-two-numbers/ |
3030
| [Clone Graph ](/LeetcodeProblems/Algorithms/Clone_Graph.js) | Medium | https://leetcode.com/problems/clone-graph/ |
3131
| [Coin Change ](/LeetcodeProblems/Algorithms/Coin_Change.js) | Medium | https://leetcode.com/problems/coin-change/ |
32+
| [Container With Most Water](/LeetcodeProblems/Algorithms/Container_With_Most_Water.js) | Medium | https://leetcode.com/problems/container-with-most-water/ |
3233
| [Design Circular Deque ](/LeetcodeProblems/Algorithms/Design_Circular_Deque.js) | Medium | https://leetcode.com/problems/design-circular-deque/
3334
| [Escape The Ghosts](/LeetcodeProblems/Algorithms/Escape_The_Ghosts.js) | Medium | https://leetcode.com/problems/escape-the-ghosts/ |
3435
| [Find All Anagrams in a String](/LeetcodeProblems/Algorithms/Find_Anagrams.js) | Medium | https://leetcode.com/problems/find-all-anagrams-in-a-string/ |

0 commit comments

Comments
 (0)