Unique Paths II Solutions in C++
Number 63
Difficulty Medium
Acceptance 34.6%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/unique-paths-ii/// Author : Hao Chen// Date : 2014-06-25#include <iostream>#include <vector>using namespace std;//As same as DP solution with "Unique Path I", just need to consider the obstacles.int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {vector<vector<unsigned int>> v (row, vector<unsigned int>(col, 0));unsigned int max=0;for (int i=0; i<obstacleGrid.size(); i++){for (int j=0; j<obstacleGrid[0].size(); j++){if(obstacleGrid[i][j] == 1){max = v[i][j] = 0;} else {if (i>0 && j>0) {max= v[i][j] = v[i-1][j] + v[i][j-1];}else if(i>0){max = v[i][j] = v[i-1][j];}else if(j>0){max = v[i][j] = v[i][j-1];}else{max = v[i][j] = 1 ;}}}}return max;}// the previous implemetation has too many if-else// the following dynamic programming is much more easy to readint uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {int row = obstacleGrid.size();int col = obstacleGrid[0].size();vector< vector <unsigned int> > dp (row, vector<unsigned int>(col, 0));dp[0][0] = obstacleGrid[0][0] ? 0 : 1;for (int r=1; r<row; r++) {dp[r][0] = obstacleGrid[r][0] ? 0 : dp[r-1][0];}for (int c=1; c<col; c++) {dp[0][c] = obstacleGrid[0][c] ? 0 : dp[0][c-1];}for (int r=1; r<row; r++) {for (int c=1; c<col; c++) {dp[r][c] = obstacleGrid[r][c] == 1 ? 0 : dp[r][c-1] + dp[r-1][c];}}return dp[row-1][col-1];}
C++ solution by pezy/LeetCode
#include <vector>#include <algorithm>#include <iostream>#include <iterator>using std::vector; using std::prev;class Solution {public:int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) {if (obstacleGrid.empty()) return 0;vector<int> vec(obstacleGrid.front().cbegin(), obstacleGrid.front().cend());auto flag = std::find(vec.begin(), vec.end(), 1);std::fill(vec.begin(), flag, 1);std::fill(flag, vec.end(), 0);for (auto line = std::next(obstacleGrid.cbegin()); line != obstacleGrid.cend(); ++line) {auto iter = vec.begin();for (auto i : *line) {if (i) *iter = 0;else if (iter != vec.begin()) *iter += *prev(iter);++iter;}}return vec.back();}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/unique-paths-ii//// Author : liuyubobobo/// Time : 2018-10-25/// Updated: 2019-09-24#include <iostream>#include <vector>using namespace std;/// Dynamic Programming/// Time Complexity: O(m*n)/// Space Complexity: O(m*n)class Solution {public:int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {int m = obstacleGrid.size();if(!m) return 0;int n = obstacleGrid[0].size();if(!n || obstacleGrid[0][0])return 0;vector<vector<long long>> dp(m, vector<long long>(n, 1ll));dp[0][0] = 1;for(int j = 1; j < n; j ++)if(obstacleGrid[0][j])dp[0][j] = 0;elsedp[0][j] = dp[0][j - 1];for(int i = 1; i < m; i ++)if(obstacleGrid[i][0])dp[i][0] = 0;elsedp[i][0] = dp[i - 1][0];for(int i = 1; i < m; i ++)for(int j = 1; j < n; j ++)if(obstacleGrid[i][j])dp[i][j] = 0;elsedp[i][j] = dp[i - 1][j] + dp[i][j - 1];return dp[m - 1][n - 1];}};int main() {return 0;}