Skip to content

Instantly share code, notes, and snippets.

@dabsclement
Created November 11, 2020 21:11
Show Gist options
  • Save dabsclement/7c690b2467efa5a43862f1ff9564795e to your computer and use it in GitHub Desktop.
Save dabsclement/7c690b2467efa5a43862f1ff9564795e to your computer and use it in GitHub Desktop.
Container With Most Water
/**
* @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