LeetCode 11- Container With Most Water(逻辑)

版权声明:转载请注明出处 https://blog.csdn.net/FGY_u/article/details/84319964

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

概述: 给定一些木板的高度,求哪两块木板底乘高为最大值。

思路: i 和 j 从两边开始走,如果他们不能提高更高的高度,就进行忽略,记住是小于等于,否则就不往前走了。

class Solution {
public:
    int maxArea(vector<int>& height) {
        int water = 0;
        int i = 0, j = height.size() - 1;
        while(i < j)
        {
            int h = min(height[i], height[j]);
            water = max(water, h * (j - i));
            while(height[i] <= h && i < j) i++;
            while(height[j] <= h && i < j) j--;
            
        }
        return water;
    }
};

猜你喜欢

转载自blog.csdn.net/FGY_u/article/details/84319964