码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Unique Paths II

时间:2015-06-03 00:43:14      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

Well, this problem is in fact the same to Unique Paths in spirit. The introduction of obstacles only changes the boundary conditions and make some points unreachable (need not be updated).

Denote the number of paths to arrive at point (i, j) to be P[i][j], then the state equation is still P[i][j] = P[i - 1][j] + P[i][j - 1]. Of course, this update only happens at points without obstacles (obstacleGrid[i][j] != 1). Moreover, the boundary values are a bit different. In theUnique Paths problem, we initialize P[0][j] and P[i][0] to be 1 for all valid i and j. Now, due to the appearance of obstacles, some boundary points are no longer reachable and need to be initialized to 0. For example, if obstacleGrid is like [0, 0, 1, 0, 0], then the last two points are not reachable and need to be initialized to be 0.

Now we can easily have the following (unoptimized) code.

 1     int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
 2         int m = obstacleGrid.size();
 3         int n = obstacleGrid[0].size();
 4         vector<vector<int> > path(m, vector<int> (n, 0));
 5         for (int j = 0; j < n; j++)
 6             if (obstacleGrid[0][j] != 1) path[0][j] = 1;
 7             else break;
 8         for (int i = 0; i < m; i++)
 9             if (obstacleGrid[i][0] != 1) path[i][0] = 1;
10             else break;
11         for (int i = 1; i < m; i++)
12             for (int j = 1; j < n; j++)
13                 if (obstacleGrid[i][j] != 1)
14                     path[i][j] = path[i - 1][j] + path[i][j - 1];
15         return path[m - 1][n - 1];
16     }

Well, the solution is accepted now. But, the above solution has some obvious redundancy. There are two major concerns:

  1. Each time when we update path[i][j], we only need path[i - 1][j] (at the same column) and path[i][j - 1] (at the previous left column), so it is unnecessary to maintain the full m*n matrix. Maintaining two columns are enough.
  2. There are some cases that the loop can be terminated earlier. Suppose the obstacleGridis like [[0, 1, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0]], then we can see that it is impossible to reach the right-down corner. Suppose we update path column by column, then after updating the second column, the fact is obvious since the number of paths to reach each element in the current column are all 0.

Taken these two problems into consideration, we have the following optimized code.

 1     int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
 2         int m = obstacleGrid.size();
 3         int n = obstacleGrid[0].size();
 4         vector<int> pre(m, 0);
 5         vector<int> cur(m, 0);
 6         for (int i = 0; i < m; i++) {
 7             if (obstacleGrid[i][0] != 1)
 8                 pre[i] = 1;
 9             else break;
10         }
11         for (int j = 1; j < n; j++) {
12             bool flag = false;
13             if (obstacleGrid[0][j] != 1) {
14                 cur[0] = pre[0];
15                 if (cur[0]) flag = true;
16             }
17             for (int i = 1; i < m; i++) {
18                 if (obstacleGrid[i][j] != 1) {
19                     cur[i] = cur[i - 1] + pre[i];
20                     if (cur[i]) flag = true;
21                 }
22             }
23             if (!flag) return 0;
24             pre = cur;
25             fill(cur.begin(), cur.end(), 0);
26         }
27         return pre[m - 1];
28     }

A final note, the introduction of early terminating in the above code seems to have almost no influence in the running time. Personally I guess it is due to that the OJ has no test cases like this:

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

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

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

...

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

[LeetCode] Unique Paths II

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4548049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!