Brush title leetcode notes - different paths

Pro 62. different paths

Subject description:

A robot located in the upper left corner of a mxn grid 

The robot can only move one step to the right or down. Robot trying to reach the lower right corner of the grid

Q. How many different paths there are in total?

Problem-solving ideas:

1.

This is the most obvious one subject combination arrangement, come from the upper left and lower-right corner to the right the number of steps to go down are fixed, so that the selected n (m-1) + (n -1) in step -1 moving towards the lower deck of a combination of problems (as may be selected m-1-step to the right, the same). Thus, a total number of different paths .

code show as below:

// permutations method, can be understood as a total of m + n-2 leaving step, wherein moving towards the lower deck selected m-1
     // use double, which will cause an overflow when m, n large 
    public  int uniquePaths ( int m, int n-) 
    { 
        Double Result = factorial (n-m-2 +,. 1-n-) / factorial (m-1,0 );
         // System.out.println (Result); 
        return ( int ) Result; 
    } 

    // factorial function 
    public  Double factorial ( int Start, int End) {
         Double RES =. 1 ;
         for ( Double I = Start; I> End; i--  ) {
            RES= res*i;
        }

        return res;
    }

2.

The second idea is a dynamic programming algorithm, in every position, it is a point above take a step down, or it points to the left go one step further to the right. The first line of the first column and all points have only one path to reach.

All to achieve the following:

// m * n grid i.e. n rows and m columns
     // all paths come to the position [i] [j] is equal to the full path to the position [i-1] [j] plus the location [i] [j-1] all paths 
    public  int uniquePaths1 ( int m, int n-) {
         int [] [] = paths new new  int [n-] [m];
         // first row 
        for ( int I = 0; I <m; I ++) paths [ 0] [I] =. 1 ;
         // first column 
        for ( int J = 0; J <n-; J ++) Paths [J] [0] =. 1 ;
         for ( int I =. 1; I <m; I ++ ) {
             for ( int J =. 1; J <n-; J ++ ) {
                paths[j][i] = paths[j][i-1] + paths[j-1][i];
            }
        }
        return paths[n-1][m-1];
    }

Pro63. Different paths ||

Subject description:

62 and similar questions, just pay more obstacles in the grid. Input is a two-dimensional array to represent the grid, the grid position of the obstacle and empty respectively  1 and  0 represented.

Now consider the grid obstructions. So how many different paths from top left to bottom right corner there will be?

Problem-solving ideas:

In the case of obstructions, if a position is disorder, the point can not be reached, the number of paths to reach all the points set to 0. In addition, during initialization of the first row and first column, if there is a grid for the barrier, then its right / bottom of the grid are unreachable.

Code:

public int uniquePathsWithObstacles(int[][] obstacleGrid){
        //若数组为空
        if(obstacleGrid.length == 0) return 0;
        
        int len = obstacleGrid.length;
        int wid = obstacleGrid[0].length;
        int[][] paths = new int[len][wid];
        
        //第一列
        for(int i=0; i<len; i++){
            if(obstacleGrid[i][0] == 1) break;
            paths[i][0] = 1;
        }
        //The first row 
        for ( int J = 0; J <WID; J ++ ) {
             IF (obstacleGrid [0] [J] ==. 1) BREAK ; 
            Paths [ 0] [J] =. 1 ; 
        } 
        for ( int I =. 1; I <len; I ++ ) {
             for ( int J =. 1; J <WID; J ++ ) {
                 // if the point of disorder, the point is reached the number of paths is 0 
                IF (obstacleGrid [I] [J] ==. 1 ) Paths [I] [J] = 0 ;
                 the else { 
                    Paths [I] [J] = Paths [. 1-I] [J] + Paths [I] [J-. 1 ]; 
                } 
            } 
        }
        return paths[len-1][wid-1];
    }

 

 

Guess you like

Origin www.cnblogs.com/yingying7/p/11230929.html