Joined July 2013
·

Luis Alberto Gudiño Salinas

Guadalajara Area, Mexico
·

Posted to Trapping Rain Water over 1 year ago

mine is faster

int landscape[] = {0,1,0,2,1,0,1,3,2,1,2,1};
int units = 0 ;
int startBarrrerPos = 0;

int size = sizeof(landscape)/ sizeof(int);

for(int i = 0; i < size-1; i++)
{
if ( landscape[i] >= landscape[startBarrrerPos])
{
startBarrrerPos=i;
continue;
}

if ( landscape[i+1] > landscape[i])
{
    int increment = (min(landscape[i+1], landscape[startBarrrerPos])) - landscape[i];

    if ( landscape[i+1]>= landscape[startBarrrerPos] )
        units+= increment * ((i+1 ) -(startBarrrerPos+1));
    else 
        units+=  increment;
}

}

Achievements
1 Karma
0 Total ProTip Views