Total number of different staircase that can made from N boxes | Function to find the total number of different staircase that can made from N boxes ; DP table , there are two states . First describes the number of boxes and second describes the step ; Initialize all the elements of the table to zero ; Base case ; When step is equal to 2 ; When step is greater than 2 ; Count the total staircase from all the steps ; Driver Code
function countStaircases ( N ) { let memo = new Array ( N + 5 ) ; for ( let i = 0 ; i < N + 5 ; i ++ ) { memo [ i ] = new Array ( N + 5 ) ; for ( let j = 0 ; j < N + 5 ; j ++ ) { memo [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i <= N ; i ++ ) { for ( let j = 0 ; j <= N ; j ++ ) { memo [ i ] [ j ] = 0 ; } } memo [ 3 ] [ 2 ] = memo [ 4 ] [ 2 ] = 1 ; for ( let i = 5 ; i <= N ; i ++ ) { for ( let j = 2 ; j <= i ; j ++ ) { if ( j == 2 ) { memo [ i ] [ j ] = memo [ i - j ] [ j ] + 1 ; } else { memo [ i ] [ j ] = memo [ i - j ] [ j ] + memo [ i - j ] [ j - 1 ] ; } } } let answer = 0 ; for ( let i = 1 ; i <= N ; i ++ ) answer = answer + memo [ N ] [ i ] ; return answer ; } let N = 7 ; document . write ( countStaircases ( N ) ) ;