Friday 17 November 2023

// Grid Unique Paths-->2-D dp concept(Tabulation)

// tc = O(mxn) 

// sc = O(mxn) --> extra dp array


import java.util.*;


class dp {

     public static int f(int dp[][], int m, int n)

     {

        for(int i=0;i<m;i++)

        {

            for(int j=0;j<n;j++)

            {

                if(i==0&&j==0) {dp[0][0]=1;continue;}

            else

            {

                int up=0;int left=0;

               if(i>0)

                up = dp[i-1][j];

                if(j>0)

                left = dp[i][j-1];

                dp[i][j] = up+left;

            }

            }

        }

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

     }


    public static void main(String args[]) {

        int m = 3;

        int n = 2;

        int dp[][] = new int[m][n];

        // Initialize the DP array with -1 to indicate uncomputed values

        for (int[] row : dp)

            Arrays.fill(row, -1);

        System.out.println(f(dp,m,n));

    }

}

 

No comments:

Post a Comment

Good thoughtful question on Binary search on answers

Problem link:  https://leetcode.com/problems/maximize-score-of-numbers-in-ranges/description/ Solution: //import java.util.Arrays; class So...