LeetCode) 11. Container With Most Water

유병수·2023년 5월 17일
0

배열안에 들어있는 높이와 가로의 길이로 넓이가 가장 큰 값을 구하는 문제.

투포인터로 시작과 끝에서 시작을 한다. 높이가 작은 쪽이면 한칸 앞으로 이동하면서 maxValue를 갱신하는 방식으로 해결

class Solution {
    public int maxArea(int[] height) {
        
        int start = 0;
        int end = height.length-1;

        int maxValue = 0;

        while(start < end){
            int firstHeight = height[start];
            int secondHeight = height[end];
            
            if(firstHeight > secondHeight){
                
                maxValue = Math.max(maxValue,(end - start) * secondHeight);
                end -=1;

            }else{
                
                maxValue = Math.max(maxValue,(end - start) * firstHeight);
                start +=1;
            }

        }

        return maxValue;
    }
}

0개의 댓글