每日一题 2019 - 04 - 05
题目:
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 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
Example 1:
1 | Input: m = 3, n = 2 |
Example 2:
1 | Input: m = 7, n = 3 |
解法:
这是一道基础的动态规划类的问题,需要找出对应的等式递推关系就可以解决问题;
思路:
- 到达
[i,j]
位置的 robot 必然是从[i,j-1]
或者[i-1,j]
位置到来,那么证明到这个位置可以有dp[i-1,j] + [i,j-1]
种走法
所以从上可知:递推公式为 dp[i,j] = dp[i-1,j] + dp[i,j-1]
代码:
1 | class Solution { |