LeetCode 11: 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 <= 105
  • 0 <= height[i] <= 104

範例

  • 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

解法一:暴力解

步驟詳解

  1. 使用一個巢狀迴圈來遍歷所有的可能組合,並在遍歷過程中,持續更新最大容量 max_water 的數值。
  2. 遍歷結束後回傳 max_water

程式碼

def container_with_most_water(height: List[int]) -> int:
    max_water = 0
    for i, left_bound in enumerate(height):
        for j, right_bound in enumerate(height):
            water = min(left_bound, right_bound) * (j - i)
            max_water = max(max_water, water)

    return max_water
Python

複雜度分析

  • 時間複雜度:O(N2),其中 Nheight 的陣列長度。
  • 空間複雜度:O(1)

解法二:雙指針

解題思路

透過觀察,我們可以發現真正影響一個區域的裝水量多寡的主要條件是兩側邊界中較低的那一側:

area = min(left\_bound, right\_bound) \times width

由於我們的目標是最大化儲存水量,因此我們可以在遍歷的過程中,持續透過只移動兩側中較低的那側邊界來提升搜索效率。

步驟詳解

  1. 初始化變數 max_water 作為目前最大儲存水量。並建立兩個指針 ij,分別從陣列的最左側及最右側開始向內移動
  2. 當左指針 i 始終在右指針 j 的左側的時候,持續進行迭代檢查:
    1. 首先計算目前範圍的儲存水量 min(height[i], height[j]) * (j - i)
    2. 若當前水量大於目前最大儲存水量 max_water,將其更新。
    3. 接著移動較低那側的邊界。
  3. 迭代結束後,回傳最大儲存水量 max_water

程式碼

def container_with_most_water(height: List[int]) -> int:
    max_water = 0
    i, j = 0, len(height) - 1

    while i < j:
        water = min(height[i], height[j]) * (j - i)
        max_water = max(max_water, water)
        if height[i] <= height[j]:
            i += 1
        else:
            j -= 1

    return max_water
Python

複雜度分析

  • 時間複雜度:O(N),其中 Nheight 的陣列長度。
  • 空間複雜度:O(1)

參考資料

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Table of Contents

Related Posts

LeetCode 657: Robot Return to Origin

There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

You are given a

LeetCode 136: Single Number

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

LeetCode 200: Number of Islands

Given an m x n 2D binary grid grid which represents a map of ‘1’s (land) and ‘0’s (water), return the number of islands.

An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.