Measuring Cup Problem(量杯问题)

Posted Dream_it_possible!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Measuring Cup Problem(量杯问题)相关的知识,希望对你有一定的参考价值。

Question

        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 line  i is at(i,ai) and(i,0), which together with x-axis forms a container, such that the container contains the most water.

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:

Input:[1,8,6,2,5,4,8,3,7]

Output: 49

Implementation approach

        Calculate all possible the capacity of the water, and then use a varible to store the maximum capacity.

private static int calculateMaxCapacity(int[] arr) {
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                int diff;
                int high;
                if (arr[i] > arr[j]) {
                    high = arr[j];
                } else {
                    high = arr[i];
                }
                diff = Math.abs(j - i);
                if (high * diff > max) {
                    max = high * diff;
                }
            }
        }
        return max;
    }

print result:

         We can find it is easy to use dual for loop to finish algorithm, but the time complexity is o(n2), so can we find the better way?

Better solution

        Problem reduction,when the length of the x-axis is fixed ,we need taller colum to hold more water.

        when arr[left] > arr[right], we should  substract the right pointer by one,else increase the left pointer by one.

  private static int betterMethod(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        int max = 0;
        while (left < right) {
            max = Math.max(max, Math.min(arr[left], arr[right]) * (right - left));
            if (arr[left] > arr[right]) {
                right--;
            } else {
                left++;
            }
        }
        return max;
    }

print result:

 It is obvios that this method run faster than first method above. the time complexity is o(n), the space complexity is o(1).

Complete Code

package leetcode100;

import java.util.Map;

/**
 * @author bingbing
 * @date 2021/8/13 0013 15:24
 * 容器水容量问题
 */
public class ContainerWaterProblem {


    public static void main(String[] args) {
        int[] arr = new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7};
        int result = calculateMaxCapacity(arr);
        System.out.println("maximum capacity is :" + result);
        int another = betterMethod(arr);
        System.out.println("maximum capacity is :" + another);

    }

    /**
     * 计算所有的盛水容量的可能,然后用一个变量保存最大的容量。
     * calculate  all possible capacities of water,and then use a variable to store the maximum capacity.
     *
     * @param arr
     * @return
     */
    private static int calculateMaxCapacity(int[] arr) {
        int max = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                int diff;
                int high;
                if (arr[i] > arr[j]) {
                    high = arr[j];
                } else {
                    high = arr[i];
                }
                diff = Math.abs(j - i);
                if (high * diff > max) {
                    max = high * diff;
                }
            }
        }
        return max;
    }


    /**
     * 更优解: 问题降维,当x坐标的所表示的长度固定时,那么我们需要更高的柱子才能盛放更多的水
     * better method: Problem reduction, when the length of x-axis is fixed, we need taller column to hold more water.
     */
    private static int betterMethod(int[] arr) {
        int left = 0;
        int right = arr.length - 1;
        int max = 0;
        while (left < right) {
            max = Math.max(max, Math.min(arr[left], arr[right]) * (right - left));
            if (arr[left] > arr[right]) {
                right--;
            } else {
                left++;
            }
        }
        return max;
    }

}

以上是关于Measuring Cup Problem(量杯问题)的主要内容,如果未能解决你的问题,请参考以下文章

Eming cup Problem D. Game of numbers

Codeforces 855C. Helga Hufflepuff's Cup----树形DP

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem A - B

Gym 101194L / UVALive 7908 - World Cup - [三进制状压暴力枚举][2016 EC-Final Problem L]

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力(

XVII Open Cup named after E.V. Pankratiev. Eastern Grand Prix. Problem F. Buddy Numbers 贪心数论构造