LeetCode / Container With Most Water

LeetCode / Container With Most Water

Problem

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
    def maxArea(self, height: List[int]) -> int:
        # Init vars
        max_area = 0
        start_index = 0
        end_index = len(height) - 1

        while start_index != end_index:
            # Get height
            start_height = height[start_index]
            end_height = height[end_index]

            # Find min height
            min_height = 0
            if start_height > end_height:
                min_height = end_height
            else:
                min_height = start_height
            
            # Get area and compare with max_area
            area = min_height * (end_index - start_index)
            if area > max_area:
                max_area = area

            # Move index of min height
            if start_height > end_height:
                end_index = end_index - 1
            else:
                start_index = start_index + 1
            
        return max_area
Solution 1
  • Description
    • List의 양쪽으로 부터 하나씩 Index를 조정하면서 가장 넓은 영역을 탐색
    • Height가 작은 Index를 하나씩 움직이며 넓이를 탐색
  • Time Complexity
    • O(n)
  • Space Complexity
    • O(1)