1. 程式人生 > >leetcode個人題解——#11 Container with most water

leetcode個人題解——#11 Container with most water

lee most 中間 size etc 當前 ner 計算 area

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

思路:先計算最寬的面積,當中間每一個容器高度小於當前兩端最低點時,肯定是不符合條件的,因為寬也減少了,略過即可。

leetcode個人題解——#11 Container with most water