11. Container With Most Water

Hill K·2022년 8월 30일
0

Algorithm

목록 보기
9/11

Example 1

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.

Example 2

Input: height = [1,1]
Output: 1

조건

n == height.length
2 <= n <= 105
0 <= height[i] <= 104

class Solution:
    def maxArea(self, height: List[int]) -> int:
        l, r = 0, len(height)-1

        out = 0
        while l < r:
            width = min(height[l], height[r])
            area = width * (r - l)
            out = max(out, area)
            if height[l] < height[r]:
                l += 1
            else:
                r -= 1
        return out
profile
안녕하세요

0개의 댓글