11.盛最多水的容器-双指针

class Solution {
    
    
public:
    int maxArea(vector<int>& height) {
    
    
        int l_ptr = 0, r_ptr = height.size()-1, max_height = 0, new_height;
        while(l_ptr < r_ptr){
    
    
            new_height = (r_ptr - l_ptr) * min(height[l_ptr], height[r_ptr]);
            if(max_height < new_height) max_height = new_height;
            if(height[l_ptr] > height[r_ptr])
                r_ptr--;
            else
                l_ptr++;
        }
        return max_height;
    }
};

Accepted
60/60 cases passed (64 ms)
Your runtime beats 92.39 % of cpp submissions
Your memory usage beats 81.7 % of cpp submissions (57.5 MB)

猜你喜欢

转载自blog.csdn.net/The_Dan/article/details/121759782