Lintcode_115_different paths II, lintcode_115ii

Source: Internet
Author: User

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];            }};

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.