Skip to content

Commit

Permalink
add: container-with-most-water
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrineKim committed Jan 16, 2025
1 parent e428ebe commit c2bf12a
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions container-with-most-water/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 시간복잡도: O(n)
// 공간복잡도: O(1)

/**
* @param {number[]} height
* @return {number}
*/
var maxArea = function (height) {
let maxWater = 0;
let left = 0;
let right = height.length - 1;

while (left < right) {
const lowerHeight = Math.min(height[left], height[right]);
const width = right - left;
maxWater = Math.max(maxWater, lowerHeight * width);

if (height[left] < height[right]) {
left++;
} else {
right--;
}
}

return maxWater;
};

0 comments on commit c2bf12a

Please sign in to comment.