11. Container With Most Water - Medium

前往題目

之前寫的文章

思路

  1. 2 pointers
  2. 只移動短的那邊,才有機會有更大的值

Code

class Solution {
    public int maxArea(int[] height) {
        int l = 0, r = height.length - 1;

        int res = Integer.MIN_VALUE;

        while (l < r) {
            // Current square height
            int h = Math.min(height[l], height[r]);
            // Area
            int area = h * (r - l);

            // Store larger area
            if (area > res) {
                res = area;
            }

            // Move pointers
            if (height[l] > height[r]) {
                --r;
            } else {
                ++l;
            }
        }

        return res;
    }
}

11. Container With Most Water - Medium
https://f88083.github.io/2024/01/31/11-Container-With-Most-Water-Medium/
作者
Simon Lai
發布於
2024年1月31日
許可協議