Complete-Preparation

πŸŽ‰ One-stop destination for all your technical interview Preparation πŸŽ‰

View the Project on GitHub

576. Out of Boundary Paths 🌟🌟

There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.

Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.

Recursion (TLE)

class Solution {
    vector<vector<int>> dirs = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
public:
    int findPaths(int m, int n, int maxMove, int startRow, int startColumn)
    {
        if (startRow < 0 || startRow >= m || startColumn < 0 || startColumn >= n)
            return 1;
        if (maxMove == 0)
            return 0;
        int ans = 0;
        for (int i = 0; i < 4; i++) {
            int nr = startRow + dirs[i][0];
            int nc = startColumn + dirs[i][1];
            ans += (findPaths(m, n, maxMove - 1, nr, nc) % MOD);
        }
        return ans;
    }
};

Memorization (AC)

class Solution {
    vector<vector<int>> dirs = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
    int MOD = 1000000007;

    int helper(int m, int n, int maxMove, int startRow, int startColumn, vector<vector<vector<int>>>& memo)
    {
        if (startRow < 0 || startRow >= m || startColumn < 0 || startColumn >= n)
            return 1;
        if (memo[startRow][startColumn][maxMove] != -1)
            return memo[startRow][startColumn][maxMove];
        if (maxMove == 0)
            return 0;
        long long ans = 0;
        for (auto dir : dirs) {
            int nr = startRow + dir[0];
            int nc = startColumn + dir[1];
            ans += helper(m, n, maxMove - 1, nr, nc, memo) % MOD;
        }
        memo[startRow][startColumn][maxMove] = ans % MOD;
        return memo[startRow][startColumn][maxMove];
    }

public:
    int findPaths(int m, int n, int maxMove, int startRow, int startColumn)
    {
        int ans = 0;
        vector<vector<vector<int>>> memo(m, vector<vector<int>>(n, vector<int>(maxMove + 1, -1)));
        ans = helper(m, n, maxMove, startRow, startColumn, memo);
        return ans;
    }
};

Tabulation (Bottom-Up) (AC)

class Solution {
    vector<vector<int>> dirs = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
    int MOD = 1000000007;

public:
    int findPaths(int m, int n, int maxMove, int startRow, int startColumn)
    {
        auto isOutOfBounds = [&](int m, int n, int r, int c) { return r < 0 || r >= m || c < 0 || c >= n; };
        vector<vector<vector<uint>>> dp(m, vector<vector<uint>>(n, vector<uint>(maxMove + 1, 0)));
        for (int i = 1; i <= maxMove; i++) {
            for (int j = 0; j < m; j++) {
                for (int k = 0; k < n; k++) {
                    for (auto dir : dirs) {
                        int r = j + dir[0];
                        int c = k + dir[1];
                        if (isOutOfBounds(m, n, r, c)) {
                            dp[j][k][i]++;
                            dp[j][k][i]%=MOD;
                        } else {
                            dp[j][k][i] += (dp[r][c][i - 1] % MOD);
                        }
                    }
                }
            }
        }
        return dp[startRow][startColumn][maxMove] % MOD;
    }
};

Must Read: