題目:
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.
思路分析:
又是動態計劃問題。
開1個f[m][n]的數組,數組元素初始化為1,遞推公式f[i][j] = f[i⑴][j] + f[i][j⑴],空間時間復雜度O(m*n)。
(可以將f[m][n]理解成為從f[0][0]到達f[m][n]的路徑個數。那很自然的就會f[i][j] = f[i⑴][j] + f[i][j⑴]。有感覺遞推公式還不是能很好想出來的,繼續加強訓練吧!)
C++參考代碼:
所以利用楊輝3角,開1個f[n]的數組,數組元素初始化為1,遞推公式f[i]+=f[i⑴],空間時間復雜度O(n)。