Skip to content

Latest commit

 

History

History
23 lines (20 loc) · 741 Bytes

算法-数组-对撞指针-最大蓄水.md

File metadata and controls

23 lines (20 loc) · 741 Bytes

https://leetcode.com/problems/container-with-most-water/

给出一个非负整数数组,每一个整数表示一个竖立在坐标轴x位置的一堵高度为该整数的墙,选择两堵墙,和x轴构成的容器可以容纳最多的水

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

欢迎光临我的博客,发现更多技术资源~