// 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