Leet Code ques Unique Paths

Question is same but we are getting the wrong answer there

int uniquePaths(int m, int n) {
long long int ans;
vector<vector> v(m+1,vector(n+1,1));
//at each cell we can come from left or top
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
v[i][j] = v[i-1][j]+v[i][j-1];
}
}
return v[m-1][n-1];

}

Please save your code on the online IDE and share it with me here.

I have corrected your code. You started calculating from the (m,n) and calculated the paths till 0,0,
so technically you calculated the result for m+1 rows and n+1 columns whereas we needed to solve it for m rows and n columns. In order to resolve this. You can either
start from m-1 row and n-1 column and calculate till (0,0)
Or
start from m row and n column and calculate till (1,1)
I have corrected your code and here’s the accepted solution
class Solution {
public int uniquePaths(int m, int n) {

    int[][] strg = new int[m+2][n+2];
	
	for(int row=m;row>=0;row--) {
		
		for(int col=n;col>=0;col--) {
			if(row==m && col ==n) {
				strg[m][n] = 1;
			}else {
				strg[row][col] = strg[row][col+1] + strg[row+1][col];
			}
		}
	}

// correction here: should start calculating from 1,1 and not 0,0
return strg[1][1];
}
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.