Created
November 11, 2020 21:11
-
-
Save dabsclement/7c690b2467efa5a43862f1ff9564795e to your computer and use it in GitHub Desktop.
Container With Most Water
This file contains hidden or 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
/** | |
* @param {number[]} height | |
* @return {number} | |
* | |
* O(n) time | |
* O(1) space | |
*/ | |
function maxArea(height) { | |
let max = 0; | |
let i = 0; | |
let j = height.length - 1; | |
while (j > i) { | |
const a = height[i]; | |
const b = height[j]; | |
const area = Math.min(a, b) * (j - i); | |
if (area > max) max = area; | |
if (b > a) i++; | |
else j--; | |
} | |
return max; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment