62. 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?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
题意:
一个机器人位于一个m*n网格的左上角(下图中标有“开始”)。机器人在坐标点方格内只能向下或向右移动。机器人正试图到达网格的右下角(下图中标有“完成”)。求有多少种可能的路径?
思路:
动态规划,用一个辅助矩阵dp,矩阵元素值来记录到达这个单元格的所有路径条数,到达每一个单元格只能从左边或者上边到达,所以此单元格的路径数等于左边和上边单元格元素的和,所以动态转移方程:dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
1 | class Solution { |
Java Code
1 | class Solution { |