Clever post thanks to others' posts


  • 2

    This problems' related posts are all almost the same !

    It is a really typical solution. We use 2 pointers to point to the 2 end.

    And move the side that influences less.

    Here is the code :

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

  • 0
    2

    2 times AC solution

    class Solution {
    public:
        int maxArea(vector<int>& height) {
            int size_height = height.size();
            int left = 0, right = size_height-1;
            int result = 0;
            while(right - left > 0) {
                int h = min(height[left], height[right]);
                int w = right - left;
                result = max(result, h * w);
                if(height[left] < height[right])    left++;
                else right--;
            }
            return result;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.