当前位置:网站首页>Topic26——11. Container with the most water

Topic26——11. Container with the most water

2022-06-09 05:44:00 _ Cabbage_

subject : Given a length of n Array of integers for height . Yes n Vertical line , The first i The two ends of the line are (i, 0) and (i, height[i]) .
Find two of them , Make them x A container of shafts can hold the most water .
Return the maximum amount of water that the container can store .
explain : You can't tilt the container .

 Example  1

 Insert picture description here

 Input :[1,8,6,2,5,4,8,3,7]
 Output :49 
 explain : The vertical line in the figure represents the input array  [1,8,6,2,5,4,8,3,7]. In this case , The container can hold water ( In blue ) The maximum value of is  49.

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

Tips :
n == height.length
2 <= n <= 105
0 <= height[i] <= 104

class Solution {
    
    public int maxArea(int[] height) {
    
        int[] memo = new int[height.length];
        Arrays.fill(memo, 0);
        int result = 0;
        for(int i = 1; i < height.length; i++) {
    
            for(int j = 0; j < i; j++) {
    
                if(height[j] > height[i]) {
    
                    memo[i] = Math.max(memo[i], height[i] * (i - j));
                    break;
                }
                memo[i] = Math.max(memo[i], Math.min(height[j], height[i]) * (i - j));
            }     
            result = Math.max(result, memo[i]);
        }
        return result;
    }
}

Double finger needling

原网站

版权声明
本文为[_ Cabbage_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090537599167.html