Skip to content

Instantly share code, notes, and snippets.

@PantherHawk
Created December 6, 2017 22:19
Show Gist options
  • Save PantherHawk/09e08d725bc3274d084d389e01aa9820 to your computer and use it in GitHub Desktop.
Save PantherHawk/09e08d725bc3274d084d389e01aa9820 to your computer and use it in GitHub Desktop.
var maxArea = function(height) {
// declare pointer variables to move from front and back
// declare area to store max area
let maxarea = 0;
let forward = 0;
let back = height.length - 1;
// while the pointer moving forward is less than the pointer moving backwards ...
while(forward < back) {
// compare the area stored to the area formed by width and the shorter of the two lines indexed by the pointers.
maxarea = Math.max(maxarea, Math.min(height[forward], height[back]) * (back - forward));
// increment whichever pointer has a smaller line
if (height[forward] < height[back]) {
forward++
} else {
back--;
}
return maxarea;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment