param { number [ ] } height return { number } function trap ( height ) { if ( ! Array . isArray ( height ) ) { return 0 ; } let left = 0 ; let right = height . length - 1 ; let result = 0 ; let left_max = 0 , right_max = 0 ; while ( left < right ) { if ( height [ left ] < height [ right ] ) { height [ left ] >= left_max ? left_max = height [ left ] : result += ( left_max - height [ left ] ) ; ++ left ; } else { height [ right ] >= right_max ? right_max = height [ right ] : result += ( right_max - height [ right ] ) ; -- right ; } } return result ; } export { trap } ;