LeetCode - Medium - 11. Container With Most Water

柔情只为你懂 2023-01-01 02:51 239阅读 0赞

Topic

  • Array
  • Two Pointers

Description

https://leetcode.com/problems/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.

Notice that you may not slant the container.

Example 1:

8ae40936a75588238d3324740c451bad.png

  1. Input: height = [1,8,6,2,5,4,8,3,7]
  2. Output: 49
  3. 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:

  1. Input: height = [1,1]
  2. Output: 1

Example 3:

  1. Input: height = [4,3,2,1,4]
  2. Output: 16

Example 4:

  1. Input: height = [1,2,1]
  2. Output: 2

Constraints:

  • n = height.length
  • 2 <= n <= 3 * 10⁴
  • 0 <= height[i] <= 3 * 10⁴

Analysis

方法一:暴力算法


方法二:双指针算法

The max area is calculated by the following formula:

  1. S = (j - i) * min(ai, aj)

We should choose (i, j) so that S is max. Note that i, j go through the range (1, n) and j > i. That’s it.

The simple way is to take all possibilities of (i, j) and compare all obtained S. The time complexity is n * (n-1) / 2.(暴力算法)

What we gonna do is to choose all possibilities of (i, j) in a wise way. I noticed that many submitted solutions here can’t explain why when :

  • ai < aj we will check the next (i+1, j) (or move i to the right)
  • ai >= aj we will check the next (i, j-1) (or move j to the left)

(也就是保留两者中最大的下标,移动最小值的下标(i的向右移动,j的向左移动))

Here is the explaination for that:

  • When ai < aj , we don’t need to calculate all (i, j-1), (i, j-2), … Why? because these max areas are smaller than our S at (i, j)

Assume at (i, j-1) we have

  1. ai < aj
  2. S = (j - i) * min(ai, aj) = (j - i) * ai
  3. if aj-1 >= ai
  4. S' = (j - 1 - i) * min(ai, aj-1) = (j - 1 - i) * ai
  5. S > S'
  6. if aj-1 < ai
  7. S' = (j - 1 - i) * min(ai, aj-1) = (j - 1 - i) * aj-1
  8. S > S'
  9. So no matter aj-1 >= ai or aj-1 < aiS' < S always true

we don’t need to calculate Similar at (i, j-2), (i, j-3), etc.

So, that’s why when ai < aj, we should check the next at (i+1, j) (or move i to the right)

  • When ai >= aj, the same thing, all (i+1, j), (i+2, j), … are not needed to calculate.

    ai >= aj
    S = (j - i) min(ai, aj) = (j - i) aj

    if ai+1 >= aj
    S’ = (j - (i + 1)) min(ai+1, aj)
    < (j - i - 1)
    aj
    S’ < S

    if ai+1 < aj
    S’ = (j - (i + 1)) min(ai+1, aj)
    < (j - i - 1)
    ai+1
    S’ < S

    So no matter ai+1 >= aj or ai+1 < aj,S’ < S always true

We should check the next at (i, j-1) (or move j to the left)

PS.似非而是的解法啊!

Submission

  1. public class ContainerWithMostWater {
  2. // 方法一:暴力算法
  3. public int maxArea1(int[] height) {
  4. int maxCapacity = 0;
  5. for (int i = 0; i < height.length; i++) {
  6. for (int j = i + 1; j < height.length; j++) {
  7. int min = Math.min(height[i], height[j]);
  8. int distance = j - i;
  9. maxCapacity = Math.max(maxCapacity, min * distance);
  10. }
  11. }
  12. return maxCapacity;
  13. }
  14. // 方法二:双指针
  15. public int maxArea2(int[] height) {
  16. int S = 0, i = 0, j = height.length - 1;
  17. while (i < j) {
  18. S = Math.max(S, (j - i) * Math.min(height[i], height[j]));
  19. if (height[i] < height[j])
  20. i++;
  21. else
  22. j--;
  23. }
  24. return S;
  25. }
  26. }

Test

  1. import static org.junit.Assert.*;
  2. import org.junit.Test;
  3. public class ContainerWithMostWaterTest {
  4. @Test
  5. public void test() {
  6. ContainerWithMostWater obj = new ContainerWithMostWater();
  7. assertEquals(49, obj.maxArea1(new int[] { 1, 8, 6, 2, 5, 4, 8, 3, 7}));
  8. assertEquals(1, obj.maxArea1(new int[] { 1, 1}));
  9. assertEquals(16, obj.maxArea1(new int[] { 4, 3, 2, 1, 4}));
  10. assertEquals(2, obj.maxArea1(new int[] { 1, 2, 1}));
  11. assertEquals(49, obj.maxArea2(new int[] { 1, 8, 6, 2, 5, 4, 8, 3, 7}));
  12. assertEquals(1, obj.maxArea2(new int[] { 1, 1}));
  13. assertEquals(16, obj.maxArea2(new int[] { 4, 3, 2, 1, 4}));
  14. assertEquals(2, obj.maxArea2(new int[] { 1, 2, 1}));
  15. }
  16. }

发表评论

表情:
评论列表 (有 0 条评论,239人围观)

还没有评论,来说两句吧...

相关阅读