C++ 16ms AC, Two Pointer


  • 0
    P

    class Solution {
    public:
    int maxArea(vector<int>& height) {
    int i = 0, j = height.size() - 1;
    int area = min(height[i], height[j]) * (j - i);
    int len = j;
    while (i != j){
    len--;
    if (height[i] < height[j]){
    i++;
    if (height[i] <= height[i - 1]){
    continue;
    } else{
    area = max(area, min(height[i], height[j]) * len);
    }
    } else{
    j--;
    if (height[j] <= height[j + 1]){
    continue;
    } else{
    area = max(area, min(height[i], height[j]) * len);
    }
    }
    }

    	return area;
    }
    

    };


Log in to reply
 

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