작심삼일

[LeetCode] 11 | Container With Most Water | Python 본문

스터디/코테

[LeetCode] 11 | Container With Most Water | Python

yun_s 2022. 4. 5. 10:02
728x90
반응형

문제 링크: https://leetcode.com/problems/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 together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.


조건

  • n == height.length
  • 2 <= n <= $10^5$
  • 0 <= height[i] <= $10^4$

내 풀이

이렇게 연속적인 구간의 최댓값을 구하는 문제는 보통 포인터 2개를 사용해서 해결하면 된다.


코드

class Solution:
    def maxArea(self, height: List[int]) -> int:
        N = len(height)
        start, end = 0, N-1
        maxi = 0
        
        while start < end:
            maxi = max(maxi, min(height[start], height[end]) * (end-start))
            
            if height[start] < height[end]:
                start += 1
            else:
                end -= 1
        
        return maxi
728x90
반응형
Comments