A question for this problem


  • 0
    L

    Here is my code for this problem, and is O(n).

    int maxArea(int height[], int n) {
    int ret = 0;
    int area = 0;

    int i = 0 ; 
    int j = n - 1;
    
    while(i < j) {
        if(height[i] < height[j]) {
            area = height[i] * (j - i);
        } else {
            area = height[j] * (j - i);
        }
        ret = ret > area ? ret : area;
        
        if(height[i] < height[j]) i++;
        else j--;
    }
    
    return ret;
    

    }

    but, I have a question: if height[i] == height[j] , and we will go to j-- , but if height[++i] > height[--j] , then the right way is i++, how to slove this problem?


  • 1
    T

    It dose not matter,because whether ++i or --j,the area will be <= currentMaxArea, we can straightly ignore it.And we will never miss the true maxArea.


Log in to reply
 

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