Short recursion O(n) solution in C++


  • 3
    C
    int maxArea(vector<int>& height) {
        return maxAreaRec(height, 0, height.size()-1);
    }
    
    int maxAreaRec(vector<int>& height, int i, int j) {
        return i == j ? 0 : 
            (height[i] > height[j] ? max(maxAreaRec(height, i, j-1), height[j] * (j - i)) 
                : max(maxAreaRec(height, i+1, j), height[i] * (j - i)));
    }

  • 0
    G
    This post is deleted!

Log in to reply
 

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