Algorithm/📊 Problem Solving

[리트코드/Leetcode] 11 - Container With Most Water

posted by sangmin

11 - Container With Most Water

📌 문제

Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.

image

📋 코드

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

        result = 0
        while left < right:
            limit = min(height[left], height[right])
            result = max(result, (right - left) * limit)

            if height[left] == limit:
                left += 1
            else:
                right -= 1

        return result

💡 한마디

left /right 두 인덱스 중 작은 값을 limit 즉, 넓이를 구하는 높이의 경계값이라고 설정하고 풀었다.

 

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

 

'Algorithm > 📊 Problem Solving' 카테고리의 다른 글

[백준/BOJ] 2210 - 숫자판 펌프  (0) 2021.03.11
[리트코드/Leetcode] 100 - Same Tree  (0) 2021.03.11
[백준/BOJ] 1726 - 로봇  (0) 2021.03.10
[백준/BOJ] 6603 - 로또  (0) 2021.03.08
[백준/BOJ] 2407 - 조합  (0) 2021.03.08