基本动态规划题
代码:
1 int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { 2 if (obstacleGrid.empty() || obstacleGrid[0].empty()) return 0; 3 4 int m = obstacleGrid.size(); 5 int n = obstacleGrid[0].size(); 6 vector<int> sum(n, 0); 7 8 sum[n - 1] = 1; 9 for (int i = m - 1; i >= 0; i--) { 10 sum[n - 1] = obstacleGrid[i][n - 1] > 0 ? 0 : sum[n - 1]; 11 for (int j = n - 2; j >= 0; j--) 12 sum[j] = obstacleGrid[i][j] > 0 ? 0 : sum[j] + sum[j + 1]; 13 } 14 15 return sum[0]; 16 }