Jun 26, 2024
f(i, j) representing unique paths to cell (i, j) from (0, 0).(m-1, n-1) and aims to reach (0, 0).i == 0 and j == 0: Return 1 (reached destination)i < 0 or j < 0: Return 0 (out of boundary)f(i, j) = f(i-1, j) + f(i, j-1)path length = (m-1) + (n-1))*dp[m][n]) initialized to -1dp[i][j] is already computed. If yes, return the valuedp[i][j] = f(i-1, j) + f(i, j-1)m x ndp[0][0] = 1i from 0 to m-1j from 0 to n-1
up and left pathsdp[i][j] = dp[i-1][j] + dp[i][j-1], handling boundary checksarrays instead of full DP table to lower memory usageprev and curr to store transitional statesprev with curr after completing each row