Unique Paths | & ||

Unique Paths I

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

分析:

用A[i][j]表示到达点i,j可能的走法。 对于点A[i][j],它可以从上一个格子下来,也可以从左边那个格子过来。所以A[i][j] = A[i-1][j] + A[i][j-1].

 1 public class Solution {
 2     /**
 3      * @param n, m: positive integer (1 <= n ,m <= 100)
 4      * @return an integer
 5      */
 6     public int uniquePaths(int m, int n) {
 7         if (n < 0 || m < 0) return 0;
 8         if (n == 1 || m == 1) return 1;
 9         
10         int[][] paths = new int[m][n];
11 
12         for (int i = 0; i < paths.length; i++) {
13             for (int j = 0; j < paths[0].length; j++) {
14                 if (i == 0 || j == 0) {
15                     paths[i][j] = 1;
16                 } else {
17                     paths[i][j] = paths[i - 1][j] + paths[i][j - 1];
18                 }
19             }
20         }
21         return paths[m - 1][n - 1];
22     }
23 }

Another solution:

 (For clarity, we will solve this part assuming an X+1 by Y+1 grid)

Each path has X+Y steps. Imagine the following paths:

X X Y Y X (we move right on the first 2 steps, then down on the next 2, then right  for the last step)

X Y X Y X (we move right, then down, then right, then down, then right)

Each path can be fully represented by the moves at which we move right. That is, if I were to ask you which path you took, you could simply say “I moved right on step 3 and 4.” Since you must always move right X times, and you have X + Y total steps, you have to pick X times to move right out of X+Y choices. Thus, there are C(X, X+Y) paths (eg, X+Y choose X).

Unique Paths II

Follow up for "Unique Paths":

Now consider if some obstacles are added to the grids. How many unique paths would there be?

An obstacle and empty space is marked as 1 and 0 respectively in the grid.

 
Example

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.

分析:

原理同上,没有任何区别。

 1 public class Solution {
 2     /**
 3      * @param obstacleGrid: A list of lists of integers
 4      * @return: An integer
 5      */
 6     public int uniquePathsWithObstacles(int[][] obstacleGrid) {
 7         if (obstacleGrid == null || obstacleGrid.length == 0 || obstacleGrid[0].length == 0) return 0;
 8         
 9         int m = obstacleGrid.length;
10         int n = obstacleGrid[0].length;
11         
12         int[][] paths = new int[m][n];
13         
14         for (int i = 0; i < m; i++) {
15             for (int j = 0; j < n; j++) {
16                 if (obstacleGrid[i][j] == 1) {
17                     paths[i][j] = 0;
18                 } else if (i == 0 && j == 0) {
19                     paths[i][j] = 1;
20                 } else if (i == 0) {
21                     paths[i][j] = paths[i][j - 1];
22                 } else if (j == 0) {
23                     paths[i][j] = paths[i - 1][j];
24                 } else {
25                     paths[i][j] = paths[i - 1][j] + paths[i][j - 1];
26                 } 
27             }
28         }
29         return paths[m - 1][n - 1];
30     }
31 }

 

posted @ 2016-07-06 07:39  北叶青藤  阅读(164)  评论(0)    收藏  举报