Google+ Followers

Thursday, January 10, 2013

[LeetCode] Unique Paths 解题报告


Ranking: **
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.
» Solve this problem

[解题思路]
一维DP。 Step[i][j] = Step[i-1][j] + Step[i][j-1];


[Code]
1:       int uniquePaths(int m, int n) {  
2:            vector<int> maxV(n,0);  
3:            maxV[0]=1;  
4:            for(int i =0; i< m; i++)  
5:            {  
6:                 for(int j =1; j<n; j++)  
7:                 {  
8:                      maxV[j] = maxV[j-1] + maxV[j];  
9:                 }  
10:            }  
11:            return maxV[n-1];  
12:       }  



Post a Comment