Daily Algorithm-10

You are given n non-negative integers a1, a2, ..., an, and each number represents a point (i, ai) in coordinates. Draw n vertical lines in the coordinates. The two endpoints of vertical line i are (i, ai) and (i, 0). Find two of the lines so that the container they form with the x-axis can hold the most water.

Note: You cannot tilt the container, and the value of n is at least 2.

The vertical line in the figure represents the input array [1,8,6,2,5,4,8,3,7]. In this case, the maximum value that the container can hold water (represented by the blue part) is 49.
Example:
Input: [1,8,6,2,5,4,8,3,7]
Output: 49
Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/container-with -most-water

class Solution {
    public int maxArea(int[] height) {
        int i=0;int len=height.length-1;
        int maxs=0;
        while(i<len)
        {
            int aa=Math.min(height[i],height[len]);
            maxs=Math.max(maxs,aa*(len-i));
            if(height[i]<height[len])
            {
                i++;
            }
            else
            {
                len--;
            }
        }
        return maxs;
    }
}

Guess you like

Origin www.cnblogs.com/lwyy1223-/p/12728040.html