Leetcode 11. Container With Most Water

当左端线段L小于右端线段R时,咱们把L右移,这时舍弃的是L与右端其余线段(R-1, R-2, ...)组成的木桶,这些木桶是不必判断的,由于这些木桶的容积确定都没有L和R组成的木桶容积大。code

public class Solution {
    public int maxArea(int[] height) {
        int p = 0;
        int q = height.length - 1;
        
        int max = 0;
        int tempMax = 0;
        
        while (p < q) {
            if (height[p] < height[q]) {
                tempMax =  (q - p) * height[p];
                if (tempMax > max) {
                    max = tempMax;
                }
                p++;
            } else {
                tempMax =  (q - p) * height[q];
                if (tempMax > max) {
                    max = tempMax;
                }
                q--;
            }
        }
        
        return max;
    }
}

自责本身为何没作出来io

相关文章
相关标签/搜索