Leetcode#11 Container with Most Water

less than 1 minute read

Published:

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

Idea:

slide window

Solution:

class Solution {
    public:
        int maxArea(vector<int>& height) {
            int left = 0;
            int right = height.size() - 1;
            int max = (right-left) * min(height[left], height[right]);

            int tmp_max;
            while (left < right) {
                if (height[left] < height[right]) {
                    left++;
                } else if (height[left] > height[right]) {
                    right--;
                } else {
                    left++;
                    right--;
                } 
                tmp_max = (right -left) * min(height[right], height[left]);
                if (tmp_max > max)
                    max = tmp_max;
            }
            return max;
        }
};