My easy undersand and simple java solution


  • -1
    D
    public int trap(int[] height) {
            int count=0;
            int tmp=0;
            int cursor=0;
           for(int i=0;i<height.length;i++){
        	   tmp=0;
               for(int j=i+1;j<height.length;j++){
                  if(j==height.length-1 && height[j]<height[i]){
                      cursor=i;
                      i=j;
                      for(;i>=cursor;i--){
        	                tmp=0;
                            for(j=i-1;j>=cursor;j--){
                                if(height[j]>=height[i]){
                                count+= height[i]*(i-j-1)-tmp;
                                i=j+1;
                                break;
                                }
                                tmp+=height[j];
                        }
                   }
                   return count;
                  }
                   if(height[j]>=height[i]){
                       count+= height[i]*(j-i-1)-tmp;
                       i=j-1;
                       break;
                       
                   }
                    tmp+=height[j];
               }
           } 
           return count;
        }

Log in to reply
 

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