11. Container With Most Water
Solved at: Apr 4, 2025 🇲🇽
function maxArea(height) {
let biggest_num = 0;
let i = 0;
let j = height.length - 1 ;
while(i < j) {
const selected_item = Math.min(height[i], height[j]);
const result = (j - i) * selected_item;
biggest_num = result > biggest_num ? result : biggest_num;
if(selected_item === height[i]){
i++
continue
} else {
j--
}
}
return biggest_num;
};
function maxArea(height) {
let biggest_num = 0; // We create a variable to store the largest number
let i = 0; // Pointer for the left
let j = height.length - 1; // Pointer for the right
while(i < j) { // If they meet, the loop ends
const selected_item = Math.min(height[i], height[j]); // Choose the smaller number between the numbers the pointers are pointing to
const result = (j - i) * selected_item; // Calculate the distance between the two pointers and the selected smaller number
biggest_num = result > biggest_num ? result : biggest_num; // If the new number is bigger, replace biggest_num
if(selected_item === height[i]) { // Decide whether to move the left or right pointer
i++;
continue;
} else {
j--;
}
}
return biggest_num;
};