C solution


  • 0
    B

    int trap(int* height, int heightSize) {
    int unitsum=0,sum=0,top=0;
    for(int i=0;i<heightSize;i++)
    {
    if(top<height[i])
    {
    sum+=unitsum;
    unitsum=0;
    top=height[i];
    }
    else
    unitsum+=top-height[i];
    }
    top=0;
    unitsum=0;
    for(int i=heightSize-1;i>=0;i--)
    {
    if(top<=height[i])
    {
    sum+=unitsum;
    unitsum=0;
    top=height[i];
    }
    else
    unitsum+=top-height[i];
    }
    return sum;
    }


Log in to reply
 

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