Given N boxes of unit dimension, i.e. (1×1 
- The staircase must be in strictly descending order.
- Each staircase contains at least two steps. ( The total steps are equal to the breadth of the staircase.)
Examples:
Input : N = 5
Output : 2
The two staircases are the following :
Input : N = 6
Output : 3
The three staircases are the following :
If we consider total steps = 2, we can observe the fact that the number of staircases is incremented by 1 if N is incremented by 2. We can illustrate the above things from the following image:
Now, if the total steps are greater than 2 (assume, total steps = K), then we can take this thing as first create a base (base requires boxes equal to the total steps) for the staircase and put another staircase on it of steps size K and K – 1 have boxes N – K. (because K boxes already used to create base). Thus, we can solve this problem using bottom-up dynamic programming.
Below is the implementation of the above approach:
C++
| // C++ program to find the total number of// different staircase that can made// from N boxes#include <iostream>usingnamespacestd;// Function to find the total number of// different staircase that can made// from N boxesintcountStaircases(intN){    // DP table, there are two states.    // First describes the number of boxes    // and second describes the step    intmemo[N + 5][N + 5];    // Initialize all the elements of    // the table to zero    for(inti = 0; i <= N; i++) {        for(intj = 0; j <= N; j++) {            memo[i][j] = 0;        }    }    // Base case    memo[3][2] = memo[4][2] = 1;    for(inti = 5; i <= N; i++) {        for(intj = 2; j <= i; j++) {            // When step is equal to 2            if(j == 2) {                memo[i][j] = memo[i - j][j] + 1;            }            // When step is greater than 2            else{                memo[i][j] = memo[i - j][j] +                              memo[i - j][j - 1];            }        }    }    // Count the total staircase    // from all the steps    intanswer = 0;    for(inti = 1; i <= N; i++)         answer = answer + memo[N][i];        returnanswer;}// Driver Codeintmain(){    intN = 7;    cout << countStaircases(N);    return0;} | 
Java
| // Java program to find the total number of// different staircase that can made// from N boxesimportjava.util.*;classGFG{        // Function to find the total number of        // different staircase that can made        // from N boxes        staticintcountStaircases(intN)        {            // DP table, there are two states.            // First describes the number of boxes            // and second describes the step            int[][] memo=newint[N + 5][N + 5];                    // Initialize all the elements of            // the table to zero            for(inti = 0; i <= N; i++) {                for(intj = 0; j <= N; j++) {                    memo[i][j] = 0;                }            }                    // Base case            memo[3][2] = memo[4][2] = 1;                    for(inti = 5; i <= N; i++) {                for(intj = 2; j <= i; j++) {                            // When step is equal to 2                    if(j == 2) {                        memo[i][j] = memo[i - j][j] + 1;                    }                            // When step is greater than 2                    else{                        memo[i][j] = memo[i - j][j] +                                     memo[i - j][j - 1];                    }                }            }                    // Count the total staircase            // from all the steps            intanswer = 0;            for(inti = 1; i <= N; i++)                 answer = answer + memo[N][i];                     returnanswer;        }                // Driver Code        publicstaticvoidmain(String [] args)        {            intN = 7;                    System.out.println(countStaircases(N));                            }}// This code is contributed // by ihritik | 
Python 3
| # Python 3 program to find the 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 boxesdefcountStaircases(N):    # DP table, there are two states.    # First describes the number of boxes    # and second describes the step    memo =[[0forx inrange(N +5)]               fory inrange(N +5)]    # Initialize all the elements of    # the table to zero    fori inrange(N +1):        forj inrange(N +1):            memo[i][j] =0            # Base case    memo[3][2] =memo[4][2] =1    fori inrange(5, N +1) :        forj inrange(2, i +1) :            # When step is equal to 2            if(j ==2) :                memo[i][j] =memo[i -j][j] +1                        # When step is greater than 2            else:                memo[i][j] =(memo[i -j][j] +                              memo[i -j][j -1])        # Count the total staircase    # from all the steps    answer =0    fori inrange(1, N +1):        answer =answer +memo[N][i]     returnanswer# Driver Codeif__name__ =="__main__":    N =7    print(countStaircases(N))# This code is contributed# by ChitraNayal | 
C#
| // C# program to find the total number // of different staircase that can made// from N boxesusingSystem;classGFG{    // Function to find the total number // of different staircase that can // made from N boxesstaticintcountStaircases(intN){    // DP table, there are two states.    // First describes the number of boxes    // and second describes the step    int[,] memo = newint[N + 5, N + 5];    // Initialize all the elements     // of the table to zero    for(inti = 0; i <= N; i++)     {        for(intj = 0; j <= N; j++)        {            memo[i, j] = 0;        }    }    // Base case    memo[3, 2] = memo[4, 2] = 1;    for(inti = 5; i <= N; i++)    {        for(intj = 2; j <= i; j++)         {            // When step is equal to 2            if(j == 2)             {                memo[i, j] = memo[i - j, j] + 1;            }            // When step is greater than 2            else            {                memo[i, j] = memo[i - j, j] +                              memo[i - j, j - 1];            }        }    }    // Count the total staircase    // from all the steps    intanswer = 0;    for(inti = 1; i <= N; i++)         answer = answer + memo[N, i];     returnanswer;}// Driver CodepublicstaticvoidMain(){    intN = 7;    Console.WriteLine(countStaircases(N));}}// This code is contributed // by Subhadeep | 
PHP
| <?php// PHP program to find the 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 boxesfunctioncountStaircases($N){        // Initialize all the elements     // of the table to zero    for($i= 0; $i<= $N; $i++)    {        for($j= 0; $j<= $N; $j++)         {            $memo[$i][$j] = 0;        }    }    // Base case    $memo[3][2] = $memo[4][2] = 1;    for($i= 5; $i<= $N; $i++)     {        for($j= 2; $j<= $i; $j++)         {            // When step is equal to 2            if($j== 2)             {                $memo[$i][$j] = $memo[$i- $j][$j] + 1;            }            // When step is greater than 2            else            {                $memo[$i][$j] = $memo[$i- $j][$j] +                                 $memo[$i- $j][$j- 1];            }        }    }    // Count the total staircase    // from all the steps    $answer= 0;    for($i= 1; $i<= $N; $i++)         $answer= $answer+ $memo[$N][$i];     return$answer;}// Driver Code$N= 7;echocountStaircases($N);// This code is contributed// by Shivi_Aggarwal?> | 
Javascript
| <script>        // Javascript program to find the 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        functioncountStaircases(N)        {            // DP table, there are two states.            // First describes the number of boxes            // and second describes the step            let memo=newArray(N + 5);             for(let i=0;i<N+5;i++)            {                memo[i]=newArray(N+5);                for(let j=0;j<N+5;j++)                {                    memo[i][j]=0;                }            }            // Initialize all the elements of            // the table to zero            for(let i = 0; i <= N; i++) {                for(let j = 0; j <= N; j++) {                    memo[i][j] = 0;                }            }                     // Base case            memo[3][2] = memo[4][2] = 1;                     for(let i = 5; i <= N; i++) {                for(let j = 2; j <= i; j++) {                             // When step is equal to 2                    if(j == 2) {                        memo[i][j] = memo[i - j][j] + 1;                    }                             // When step is greater than 2                    else{                        memo[i][j] = memo[i - j][j] +                                    memo[i - j][j - 1];                    }                }            }                     // Count the total staircase            // from all the steps            let answer = 0;            for(let i = 1; i <= N; i++)                answer = answer + memo[N][i];                     returnanswer;           }            // Driver Code        let N = 7;        document.write(countStaircases(N));    // This code is contributed by rag2127</script> | 
4
Time Complexity: O(
).
Auxiliary Space: O(n ^ 2)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    







