When i run code this input [1,7,8] is ok,but when i submit it.It reminder me :the Last executed input: [1,7,8]


  • 0
    H

    class Solution {
    public:
    int trap(vector<int>& height) {
    int all_water = 0;
    if(height.size() == 0 || height.size() == 1 || height.size() == 2){
    return all_water;
    }else{
    int size = height.size();
    int left_max = 0;
    int right_max = 0;
    int left = 0;
    int right = height.size() -1;
    while((height[left] < height[left+1]) && left <size){
    left ++;
    }
    left_max = height[left];
    while((height[right] < height[right -1]) && right >= 0){
    right --;
    }
    right_max = height[right];

            while(left != right){
                if(left_max > right_max){
                    right = right-1;
                    while(height[right] < right_max){
                        all_water = all_water + right_max - height[right];
                        right--;
                    }
                    right_max = height[right];
                }else{
                    left = left +1;
                    while(height[left] < left_max){
                        all_water = all_water + left_max - height[left];
                        left++;
                    }
                    left_max = height[left];
                }
            }
        
        }
       
        return all_water;
    }
    

    };


Log in to reply
 

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