DEV Community

Miss Pooja Anilkumar Patel
Miss Pooja Anilkumar Patel

Posted on

1420. Leetcode solution in Cpp

class Solution {
     long long ways[55][105][55];
      const int mod = 1000000007;
  public:
      int numOfArrays(int n, int m, int num) {
          memset(ways, 0, sizeof(ways));

          for (int j = 1; j <= m; ++j) ways[1][j][1] = 1;
        for (int i = 1; i <= n; ++i) {
             for (int j = 1; j <= m; ++j) {
                for (int k = 1; k <= num; ++k) {
                    long long s = 0;
                    s = (j * ways[i-1][j][k]) % mod;
                    for (int p = 1; p < j; ++p)                         s = (s + ways[i-1][p][k-1]) % mod;
                    ways[i][j][k] = (s + ways[i][j][k]) % mod;
                }
             }
        }
        long long ans = 0;
        for (int j = 1; j <= m; ++j) {
            ans = (ans + ways[n][j][num]) % mod;
         }
        return ans;
     }
 };
Enter fullscreen mode Exit fullscreen mode

Discussion (0)