본문 바로가기

리트코드/midium

[리트코드] 11. Container With Most Water - js (투포인터)

반응형

1. 문제

https://leetcode.com/problems/container-with-most-water/

 

Container With Most Water - LeetCode

Can you solve this real interview question? Container With Most Water - You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that toget

leetcode.com

2. 코드

/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function(height) {
    let n = height.length;
    let left = 0, right = n - 1;
    let max_area = 0;
    while (left < right) {
        let area = Math.min(height[left], height[right]) * (right - left);
        max_area = Math.max(max_area, area);
        if (height[left] < height[right]) {
            left++;
        } else {
            right--;
        }
    }
    
    return max_area;
}

투포인터, DP 얘네 둘 언제 잘 풀게 될까..

 

비슷한 문제)

https://leetcode.com/problems/trapping-rain-water/description/

반응형