c++ cool one-line solution


  • 0
    G

    unreadable one-line version:

        int maxArea(vector<int>& height) {int l = 0, r = height.size()-1,maxarea = 0; while(l < r) if(height[l]>=height[r]) maxarea = max(maxarea, height[r] * ((r--)-l)); else maxarea = max(maxarea, height[l] * (r-(l++))); return maxarea;}
    

    normal version

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

Log in to reply
 

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