📌  相关文章
📜  由 N 个盒子组成的不同楼梯的总数

📅  最后修改于: 2021-09-17 16:11:02             🧑  作者: Mango

给定 N 个单位尺寸的盒子,即 (1×1 meter^2   )。任务是根据以下规则找出可以由这些盒子制成的不同楼梯的总数:

  • 楼梯必须严格按照降序排列。
  • 每个楼梯至少包含两个台阶。 (总步数等于楼梯的宽度。)

例子

如果我们考虑总步数 = 2,我们可以观察到,如果 N 增加 2,则楼梯数量增加 1。我们可以从下图说明上述事情:

现在,如果总步数大于 2(假设,总步数 = K),那么我们可以将这件事视为首先为楼梯创建一个基础(基础需要等于总步数的框)并在其上放置另一个楼梯步长 K 和 K – 1 有 N – K 框(因为 K 框已经用于创建基础)。因此,我们可以使用自底向上的动态规划来解决这个问题。

下面是上述方法的实现:

C++
// C++ program to find the total number of
// different staircase that can made
// from N boxes
#include 
using namespace std;
 
// Function to find the total number of
// different staircase that can made
// from N boxes
int countStaircases(int N)
{
    // DP table, there are two states.
    // First describes the number of boxes
    // and second describes the step
    int memo[N + 5][N + 5];
 
    // Initialize all the elements of
    // the table to zero
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= N; j++) {
            memo[i][j] = 0;
        }
    }
 
    // Base case
    memo[3][2] = memo[4][2] = 1;
 
    for (int i = 5; i <= N; i++) {
        for (int 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
    int answer = 0;
    for (int i = 1; i <= N; i++)
        answer = answer + memo[N][i];   
 
    return answer;
}
 
// Driver Code
int main()
{
    int N = 7;
 
    cout << countStaircases(N);
 
    return 0;
}


Java
// Java program to find the total number of
// different staircase that can made
// from N boxes
 
import java.util.*;
 
class GFG
{
        // Function to find the total number of
        // different staircase that can made
        // from N boxes
        static int countStaircases(int N)
        {
            // DP table, there are two states.
            // First describes the number of boxes
            // and second describes the step
            int [][] memo=new int[N + 5][N + 5];
         
            // Initialize all the elements of
            // the table to zero
            for (int i = 0; i <= N; i++) {
                for (int j = 0; j <= N; j++) {
                    memo[i][j] = 0;
                }
            }
         
            // Base case
            memo[3][2] = memo[4][2] = 1;
         
            for (int i = 5; i <= N; i++) {
                for (int 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
            int answer = 0;
            for (int i = 1; i <= N; i++)
                answer = answer + memo[N][i];
         
            return answer;
        }
         
        // Driver Code
        public static void main(String [] args)
        {
            int N = 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 boxes
def countStaircases(N):
 
    # DP table, there are two states.
    # First describes the number of boxes
    # and second describes the step
    memo = [[0 for x in range(N + 5)]
               for y in range(N + 5)]
 
    # Initialize all the elements of
    # the table to zero
    for i in range(N + 1):
        for j in range (N + 1):
            memo[i][j] = 0
         
    # Base case
    memo[3][2] = memo[4][2] = 1
 
    for i in range (5, N + 1) :
        for j in range (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
    for i in range (1, N + 1):
        answer = answer + memo[N][i]
 
    return answer
 
# Driver Code
if __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 boxes
using System;
 
class GFG
{
     
// Function to find the total number
// of different staircase that can
// made from N boxes
static int countStaircases(int N)
{
    // DP table, there are two states.
    // First describes the number of boxes
    // and second describes the step
    int [,] memo = new int[N + 5, N + 5];
 
    // Initialize all the elements
    // of the table to zero
    for (int i = 0; i <= N; i++)
    {
        for (int j = 0; j <= N; j++)
        {
            memo[i, j] = 0;
        }
    }
 
    // Base case
    memo[3, 2] = memo[4, 2] = 1;
 
    for (int i = 5; i <= N; i++)
    {
        for (int 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
    int answer = 0;
    for (int i = 1; i <= N; i++)
        answer = answer + memo[N, i];
 
    return answer;
}
 
// Driver Code
public static void Main()
{
    int N = 7;
 
    Console.WriteLine(countStaircases(N));
}
}
 
// This code is contributed
// by Subhadeep


PHP


Javascript


输出:
4

时间复杂度:O( n^2   )。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程