Simple DP Solution


  • 0
    public class Solution {
        public int uniquePaths(int m, int n) {
            int[][] dp = new int[m][n];
            dp[m-1][n-1] = 1;
            for (int row = m -1; row > -1; row--) {
                for (int col = n - 1; col > -1; col--) {
                    int right = col + 1 == n ? 0 : dp[row][col+1], down = row + 1 == m ? 0 : dp[row+1][col];
                    dp[row][col] = Math.max(dp[row][col], right + down);
                }
            }
            return dp[0][0];
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.