Day 28 (30 Days of Code)

Day 28 (30 Days of Code)

31 July 2023

Solved two more questions on dynamic programming. I am starting to see patterns in questions. Day by day things are getting easy.

My approach:

I have previously solved this type of question and that is why I was able to come up with the solution without any hints. As usual, my very first approach was to try recursion, after that, I memorized the solution and eventually came up with an iterative approach which is dynamic programming.

My solution:

  • Recursion with Memoization
#include <bits/stdc++.h>

int helper(int m, int n, int i, int j, int** arr){
    if(i>=m || j>=n){
        return 0;
    }

    if(i==m-1 && j==n-1){
        return 1;
    }

    if(arr[i][j]!=-1){
        return arr[i][j];
    }

    int right = helper(m,n,i,j+1,arr);
    int down = helper(m,n,i+1,j,arr);

    arr[i][j] = right+down;

    return right+down;
}

int uniquePaths(int m, int n) {
    // Write your code here.

    int** arr = new int*[m];

    for(int i=0;i<m;i++){
        arr[i] = new int[n];
    }

    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            arr[i][j] = -1;
        }
    }

    return helper(m,n,0,0,arr);
}
  • Dynamic Programming
int uniquePaths(int m, int n) {
    // Write your code here.

    int** arr = new int*[m];

    for(int i=0;i<m;i++){
        arr[i] = new int[n];
    }

    for(int i=0;i<m;i++){
        arr[i][0] = 1;
    }

    for(int i=0;i<n;i++){
        arr[0][i] = 1;
    }

    for(int i=1;i<m;i++){
        for(int j=1;j<n;j++){
            arr[i][j] = arr[i-1][j]+arr[i][j-1];
        }
    }

    return arr[m-1][n-1];

}

My approach:

It was a further extension of the previous question with one condition. I was able to figure out the solution but I struggled to define the base case for a dynamic programming solution eventually, on dry run, I got the solution.

My solution:

  • Recursion with Memoization
int helper(int n, int m,int i, int j, vector<vector<int>> &mat, int** arr){

    int mod = 1e9+7;

    if(i>=n || j>=m){
        return 0;
    }

    if(i==n-1 && j==m-1){
        return 1;
    }

    if(arr[i][j]!=-1){
        return arr[i][j];
    }

    if(mat[i][j]==-1){
        return 0;
    }


    long long int right = helper(n,m,i+1,j,mat,arr)%mod;
    long long int down = helper(n,m,i,j+1, mat,arr)%mod;

    arr[i][j] = (right+down)%mod;
    return arr[i][j];
}

int mazeObstacles(int n, int m, vector< vector< int> > &mat) {
    // Write your code here

    int** arr = new int*[n];

    for(int i=0;i<n;i++){
        arr[i] = new int[m];
    }

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            arr[i][j] = -1;
        }
    }

    return helper(n,m,0,0,mat,arr);
}
  • Dynamic Programming
int mazeObstacles(int n, int m, vector< vector< int> > &mat) {
    // Write your code here

    int mod = 1e9+7;

    int** arr = new int*[n];

    for(int i=0;i<n;i++){
        arr[i] = new int[m];
    }

    arr[0][0] = 1;

    for(int i=1;i<n;i++){
        if(mat[i][0]!=-1){
            arr[i][0] = arr[i-1][0];
        }else{
            arr[i][0] = 0;
        }
    }

    for(int i=1;i<m;i++){
        if(mat[0][i]!=-1){
            arr[0][i] = arr[0][i-1];
        }else{
            arr[0][i] = 0;
        }
    }


    for(int i=1;i<n;i++){
        for(int j=1;j<m;j++){
            if(mat[i][j]==-1){
                arr[i][j] = 0;
            }
            else{
                arr[i][j] = (arr[i-1][j]+arr[i][j-1])%mod;
            }
        }
    }

    return arr[n-1][m-1];
}