Accepted Solution in c


  • 0
    D
     int trap(int* height, int heightSize)
     {
    if(heightSize==1||heightSize==0)
    {return 0;
    }
    int h=0,max=0,k,count,curr;
    while(heightSize!=1)
    {
        if(height[heightSize-1]<=height[heightSize-2])
        {
            heightSize--;
        }
        else
        {
            max=heightSize-2;
            count=1;
            curr=heightSize-2;
            int sum=0;
            sum+=height[curr];
            while(curr!=0&&height[curr-1]<height[heightSize-1])
            {
               count++; 
                curr--;
                sum+=height[curr];
                if(height[max]<height[curr])
                {
                    max=curr;
                }
            }
            
            if(curr!=0)
            {
                max=curr-1;
            }
            if(curr==0)
            {
                count=heightSize-2-max;
                k=max;
                while(k>=0)
                {
                    sum-=height[k];
                    k--;
                }
                
    
            }
            
        if(height[heightSize-1]<height[max])
        {
            h+=height[heightSize-1]*count-sum;
        }
        else
        {
            h+=height[max]*count-sum;
        }
        heightSize=max+1;
    
        }
    }
    return h;
    

    }


Log in to reply
 

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