leetcode #62 in cpp

原创 2016年05月30日 22:29:10

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?


Above is a 3 x 7 grid. How many possible unique paths are there?


Solution:

Suppose if w are currently in  a cell, we can only move right or down. Thus the number of paths from this cell to the destination is the sum the number of paths to destination from the cell right next to it,  and the number of paths to the destination from the cell blow it . 

We use DP to solve this. 

dp[i][j] = number of paths of the position(i+1, j + 1) to the destination

  = dp[i+1][j] + d[[i][j+1]

The initialization involves the bottom row and rightmost column, both initialized to 1 since they can move in only 1 direction.


Code:

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> dp(m, vector<int>(n,0));//dp[i][j] = number of paths to destination
        dp[m-1][n-1] = 1;//destination to 1
        for(int i = 0; i < m && n-1 >=0; i ++){// right border to 1 
            dp[i][n-1] = 1;
        }
        for(int i = 0; i < n && m-1 >= 0; i ++){//bottom border to 1
            dp[m-1][i] = 1;
        }
        for(int i = m-2; i >=0; i --){
            for(int j = n-2; j>=0; j --){
                dp[i][j]+= dp[i][j+1] + dp[i+1][j];//current path number = num of its righr cell paths + num of its down cell paths to destination
            }
        }
        return dp[0][0];
    }
};


[LeetCode]62 不同的路径总数

Unique Paths(不同的路径总数)【难度:Medium】 A robot is located at the top-left corner of a m x n grid (marked ...
  • qq_14821023
  • qq_14821023
  • 2016年02月28日 13:31
  • 702

[LeetCode]63 不同的路径总数之二

Unique Paths II(不同的路径总数之二)【难度:Medium】 Follow up for “Unique Paths”:Now consider if some obstacles a...
  • qq_14821023
  • qq_14821023
  • 2016年02月28日 13:42
  • 355

【LeetCode】62. Unique Paths解法及注释

62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the ...
  • Jin_Kwok
  • Jin_Kwok
  • 2016年04月08日 11:50
  • 240

【leetcode算法】更新至62题

1、T104  树的深度 public class T104 {     }     class TreeNode {       int val;       TreeNode le...
  • gol_phing
  • gol_phing
  • 2015年07月07日 09:45
  • 279

leetcode || 62、Unique Paths

problem: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diag...
  • hustyangju
  • hustyangju
  • 2015年04月02日 11:24
  • 634

(Java)LeetCode-62. Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
  • u012848330
  • u012848330
  • 2016年10月06日 21:54
  • 263

LeetCode 62

Unique Paths     机器人在最左上角,它只能向右和向下走,找出所有的独一无二的路径,使它能达到最右下角位置。     对已这个题我首先的思路是递归,因为机器人只能向下和向右走,所以将下和...
  • zr1076311296
  • zr1076311296
  • 2016年06月08日 23:16
  • 2037

LeetCode-62-Unique Paths(动态规划)-Medium

题目理解: 在只允许从左向右,从上到下移动的情况下,从mxn的矩阵(0,0)点,到(m-1,n-1)点,有多少路径; 题目分析: 1. 回溯遍历会超时; 2. 动规; 解题代...
  • eddy_liu
  • eddy_liu
  • 2016年03月14日 08:51
  • 1100

LeetCode Solution(持续更新,java>c++)

Title Add Date AC Rates
  • disappearedgod
  • disappearedgod
  • 2014年04月13日 21:52
  • 8831

[leetcode] 【分治法】 50. Pow(x, n)

Implement pow(x, n). 题意 实现幂函数。 题解 使用分治法求解。 分:将n分成n/2  直到n=0时,返回1; 治:对n为偶数,返回两数相乘的结果,奇数再乘多一个x...
  • u014654002
  • u014654002
  • 2016年06月21日 00:37
  • 381
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #62 in cpp
举报原因:
原因补充:

(最多只允许输入30个字)