본문 바로가기
Problem Solving/LeetCode

[LeetCode] 11. Container With Most Water - Java

by graycode 2026. 5. 8.

 문제 링크

 

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

 

 풀이 코드

public class Solution {

    public int maxArea(int[] height) {
        int l = 0, r = height.length - 1, max = 0;
        while (l < r) {
            max = Math.max(max, Math.min(height[l], height[r]) * (r - l));
            if (height[l] < height[r]) l++;
            else r--;
        }

        return max;
    }

}

댓글