Created
December 6, 2017 22:19
-
-
Save PantherHawk/09e08d725bc3274d084d389e01aa9820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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