Sort by

recency

|

48 Discussions

|

  • + 0 comments

    O(300^2 + n * m^2)

    long mod = 1000000007;
    
    void combinatorial(vector<vector<long>>& binomial) {
        binomial[1][0] = 1;
        binomial[1][1] = 1;
        for (int n = 2; n <= 300; n++) {
            binomial[n][0] = 1;
            for (int k = 1; k <= n; k++) binomial[n][k] = (binomial[n-1][k-1] + binomial[n-1][k]) % mod;
        }
    }
    
    int gridWalking(int n, int m, const vector<int>& x, const vector<int>& D, const vector<vector<long>>& binomial) {
        vector<vector<long>> oneDimension(n+1, vector<long>(m+1)), multiDimension(n+1, vector<long>(m+1)), endPoint = {{0}};
        for (int i = 1; i <= n; i++) {
            oneDimension[i][0] = 1;
            endPoint.emplace_back(vector<long>(D[i-1] + 2));
            endPoint.back()[x[i-1]] = 1;
            for (int K = 1; K <= m; K++) {
                long total = 0, temp1 = 0, temp2;
                for (int point = 1; point <= D[i-1]; point++) {
                    temp2 = endPoint[i][point];
                    endPoint[i][point] = (temp1 + endPoint[i][point + 1]) % mod;
                    total = (total + endPoint[i][point]) % mod;
                    temp1 = temp2;
                }
                oneDimension[i][K] = total;
            }
        }
        multiDimension[1] = oneDimension[1];
        for (int i = 2; i <= n; i++) {
            multiDimension[i][0] = 1;
            for (int K = 1; K <= m; K++) {
                long temp = 0;
                for (int l = 0; l <= K; l++) temp = (temp + multiDimension[i-1][l] * (oneDimension[i][K-l] * binomial[K][l] % mod)) % mod;
                multiDimension[i][K] = temp;
            }
        }
        return multiDimension[n][m];
    }
    
    int main()
    {
        int t, n, m, k;
        vector<vector<long>> binomial(301, vector<long>(301));
        combinatorial(binomial);
        cin >> t;
        for (int i = 1; i <= t; i++) {
            vector<int> x, D;
            cin >> n >> m;
            for (int j = 1; j <= n; j++) {
                cin >> k;
                x.push_back(k);
            }
            for (int j = 1; j <= n; j++) {
                cin >> k;
                D.push_back(k);
            }
            cout << gridWalking(n, m, x, D, binomial) << '\n';
        }
    }
    
  • + 0 comments

    Walkeaze provides a wide range of Clutches online in Pakistan. Discover the stylish clutch for women online at Walkeaze. Order your favorite clutch bag for ladies online at reasonable prices.

  • + 0 comments

    here is my solution in java, javascript, python, C, C++, Csharp HackerRank Grid Walking Problem Solution

  • + 0 comments

    Here is Grid Walking problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-grid-walking-problem-solution.html

  • + 0 comments
    def gridWalking(m, x, D):
        MOD = 10 ** 9 + 7
        n = len(D)
        md = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
        for i in range(n):
            M = [[0 for _ in range(m + 1)] for _ in range(D[i] + 1)]
            for j in range(1, D[i] + 1):
                M[j][0] = 1
            for j in range(1, m + 1):
                for k in range(1, D[i] + 1):
                    M[k][j] = 0
                    if k - 1 > 0:
                        M[k][j] = (M[k][j] + M[k - 1][j - 1]) % MOD;
                    if k + 1 <= D[i]:
                        M[k][j] = (M[k][j] + M[k + 1][j - 1]) % MOD
            md[0][i + 1] = 1
            for j in range(1, m + 1):
                md[j][i + 1] = M[x[i]][j]
        c = [[0 for _ in range(m + 1)] for _ in range(m + 1)]
        for i in range(m + 1):
            c[i][0] = 1
            c[i][i] = 1
        for i in range(1, m + 1):
            for j in range(1, i):
                c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % MOD
        mdt = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
        for i in range(m + 1):
            mdt[i][1] = md[i][1]
        for i in range(n + 1):
            mdt[0][i] = 1
        for i in range(2, n + 1):
            for j in range(1, m + 1):
                mdt[j][i] = 0
                for k in range(j + 1):
                    mdt[j][i] = (mdt[j][i] + ((c[j][j - k] * ((mdt[k][i - 1] * md[j - k][i]) % MOD)) % MOD)) % MOD
        return mdt[m][n]