Lintcode_115_different paths II, lintcode_115ii
Different paths II
- Description
- Notes
- Data
- Evaluation
Follow-up questions about "different paths:
Now, how many different paths will there be if there are obstacles in the grid?
Obstacles and empty positions in the grid are represented by 1 and 0 respectively.
Notes
Both m and n cannot exceed 100
Have you ever encountered this question during a real interview? Yes, which company asked you this question? LinkedIn Amazon Airbnb Cryptic Studios Dropbox Epic Systems TinyCo Hedvig Uber Yelp Apple Yahoo Bloomberg Zenefits Twitter Microsoft Google Snapchat Facebook
Thank you for your feedback.
Example
As shown below, there is an obstacle in the 3x3 mesh:
[ [0,0,0], [0,1,0], [0,0,0]]
There are two different paths from the top left to the bottom right.
TagThe idea of the 114 question enhanced edition has not changed. The B array is referenced as the obstacle array.
class Solution {public: /* * @param obstacleGrid: A list of lists of integers * @return: An integer */ int b[101][101]; int uniquePathsWithObstacles(vector<vector<int>> &a) { // write your code here int x=a.size(); int y=a[x-1].size(); if(a[0][0]==1) return 0; for(int i=0;i<x;i++) for(int j=0;j<y;j++){ if(a[i][j]==1) b[i][j]=-1; a[i][j]=0; } int flag=0; for(int i=0;i<x;i++){ a[i][0]=1; if(b[i][0]==-1) flag=1; if(flag) a[i][0]=0; } flag=0; for(int i=0;i<y;i++){ a[0][i]=1; if(b[0][i]==-1) flag=1; if(flag) a[0][i]=0; } for(int i =1;i<x;i++) for(int j=1;j<y;j++){ a[i][j]=a[i-1][j]+a[i][j-1]; if(b[i][j]==-1) a[i][j]=0; } return a[x-1][y-1]; }};