3

I have just started learning dp and trying to solve this problem from leetcode using the same ( https://leetcode.com/problems/unique-paths/)

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

enter image description here

Here is what I tried:

public class Solution {
public int uniquePaths(int m, int n) {
    int [][] grid = new int[m][n];
    int [][] memo = new int[m][n];
    return uniquePathsHelper(grid,m,n,memo);
}

public int uniquePathsHelper(int [][] grid, int row,int col,int[][]memo){
    if(row>grid.length-1 || col>grid[0].length-1) return -1;
    if(row == grid.length-1 && col == grid[0].length-1) return 0;
    if(memo[row][col]!=0) return memo[row][col];

    if(col == grid[0].length-1) memo[row][col] = uniquePathsHelper(grid,row+1,col,memo)+1;
    if(row == grid.length-1) memo[row][col] = uniquePathsHelper(grid,row,col+1,memo)+1;
    // int rowInc = Integer.MIN_VALUE;
    // int colInc = Integer.MIN_VALUE;
    // if(row<grid.length-1) rowInc =  uniquePathsHelper(grid, row+1,col,memo);
    // if(col<grid.length-1) colInc = uniquePathsHelper(grid,row,col+1,memo);


    // if(row == grid.length-1 || col == grid[0].length-1) return 1;

    // if(row<grid.length-1) return 2;
    // if(col<grid[0].length-1) return 2;

    if(col< grid[0].length-1 && row < grid.length-1) memo[row][col] = memo[row+1][col] + memo[row][col+1];
    System.out.println("Memo["+row+"]["+col+"] = "+memo[row][col]);
    return memo[0][0];
    }
}

Sorry if this sounds very basic, I know I am missing something. Can anyone point out what is wrong with it?

Ivan Gritsenko
  • 4,166
  • 2
  • 20
  • 34
  • Possible duplicate of [Algorithm for finding all paths in a NxN grid](http://stackoverflow.com/questions/9105699/algorithm-for-finding-all-paths-in-a-nxn-grid) – aerin Mar 12 '17 at 00:24

1 Answers1

1

To solve this problem lets define a recurrent formulas for f(r,c). There might be several option to do it but lets stick with what you have in code.

  1. f(r, c) = 0 if r >= m
  2. f(r, c) = 0 if c >= n
  3. f(r, c) = 1 if r == m && c == n
  4. f(r, c) = f(r + 1, c) + f(r, c + 1)

According to the formulas what will uniquePathsHelper look like?

// actually we don't need grid at all.
// assume that we have m rows and n cols, m and n are global variables
public int uniquePathsHelper(int row, int col, int[][] memo) { 
    // 1-st and 2-d formulas
    if(row >= m || col >= n) return 0;
    // 3-d formula
    if(row == m - 1 && col == n - 1) return 1;

    if(memo[row][col] != 0) {
        // 4-th formula
        memo[row][col] = uniquePathsHelper(row, col + 1, memo) + 
            uniquePathsHelper(row + 1, col, memo);
    }
    return memo[row][col];
}

To receive an answer one should just simply call uniquePathsHelper(0, 0, memo) which means How many paths exist from (0,0)-cell to (m-1, n-1)-cell?

Ivan Gritsenko
  • 4,166
  • 2
  • 20
  • 34