💡코딩테스트/Leetcode

[ LeetCode | python ] 11. Container With Most Water

두_두 2023. 2. 1. 09:28

🌏 문제

 

Container With Most Water - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

물을 최대로 채울 수 있게 하는 기둥 두개를 구하는 문제이다.

 

👽 풀이

첫번째 접근

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

        while start < end:
            curArea = min(height[start], height[end]) * (end-start)

            if min(height[start+1],height[end]) * (end-start-1) > curArea:
                start += 1
            elif min(height[start], height[end - 1]) * (end-1-start) > curArea:
                end -=1

            else:
                return curArea

 

최종 코드

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

        while start < end:
            maxArea = max(maxArea, min(height[start], height[end]) * (end-start))

            if height[start] < height[end]:
                start += 1
            else:
                end -= 1
        return maxArea

 

728x90