Skip to content

Instantly share code, notes, and snippets.

@sAVItar02
Created October 26, 2024 18:23
Show Gist options
  • Save sAVItar02/8336dd6f4951f49fb3129953ebf83442 to your computer and use it in GitHub Desktop.
Save sAVItar02/8336dd6f4951f49fb3129953ebf83442 to your computer and use it in GitHub Desktop.
Container with most water
/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function(height) {
let l = 0;
let r = height.length - 1;
let maxArea = 0;
let area = 0;
while (l < r) {
height[l] < height[r] ? area = height[l] * (r - l) : area = height[r] * (r - l);
if(area > maxArea) maxArea = area;
height[l] < height[r] ? l++ : r--;
}
return maxArea;
};
// 2 pointers, one pointing at the start of the array and another at the end
// Calculate the current area
// If the area is more than the maxArea, then maxArea = area
// Else, More the one with the lesses magnitude
// ex: if element at left pointer is 2 and element at right pointer is 7 then increase the left pointer
// Repeat as long as left pointer < right pointer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment