由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - Leetcode上的Unique Paths II,我的code对吗?
相关主题
leetcode题目请教一个leetcode上的题
G家题讨论: harry potter 走矩阵攒人品,google电话面经
leetcode里, backtracking的time complexity怎么算,比如permutations这题目twitter电面
enumerate all unique paths of robot问个计算化学问题:怎么读GRID?
【update】cs小硕 下周g onsite 求祝福leetcode triganle 通不过。。。
matrix questionf design question 求讨论
继续分享G家phone interview题目看到个面试题,不会做……
请教leetcode上的minimum path sum有space O(M+N)的解法吗?Unique Path II
相关话题的讨论汇总
话题: grid话题: int话题: mat话题: max话题: backtrack
进入JobHunting版参与讨论
1 (共1页)
f*********m
发帖数: 726
1
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?
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.
我修改了一下leetcode的答案,不知对否?谢谢。
注:m和n是grid的size.
1.Recursive方法:
int backtrack(int r, int c, int m, int n, vector >& grid) {
if (r == m && c == n)
return 1;
if (r > m || c > n)
return 0;
if (grid[r+1][c] == 0 && grid[r][c+1] == 0)
return backtrack(r+1, c, m, n, grid) + backtrack(r, c+1, m, n, grid);
else if (grid[r+1][c] == 1 && grid[r][c+1] == 0)
return backtrack(r, c+1, m, n, grid);
else if (grid[r+1][c] == 0 && grid[r][c+1] == 1)
return backtrack(r+1, c, m, n, grid);
else
return 0;
}
2. DP (top->down):
const int M_MAX = 100;
const int N_MAX = 100;
int backtrack(vector >&grid, int r, int c, int m, int n, int mat
[][N_MAX+2]) {
if (r == m && c == n)
return 1;
if (r > m || c > n)
return 0;
if (mat[r+1][c] == -1 && grid[r+1][c] == 0)
mat[r+1][c] = backtrack(grid,r+1, c, m, n, mat);
else if (mat[r+1][c] == -1 && grid[r+1][c] == 1)
mat[r+1][c] = 0;
if (mat[r][c+1] == -1 && grid[r][c+1] == 0)
mat[r][c+1] = backtrack(grid,r, c+1, m, n, mat);
else if (mat[r+1][c] == -1 && grid[r][c+1] == 1)
mat[r][c+1] = 0;
return mat[r+1][c] + mat[r][c+1];
}
int bt(vector >&grid, int m, int n) {
int mat[M_MAX+2][N_MAX+2];
for (int i = 0; i < M_MAX+2; i++) {
for (int j = 0; j < N_MAX+2; j++) {
mat[i][j] = -1;
}
}
return backtrack(grid,1, 1, m, n, mat);
}
3. DP 2 (bottom->up):
const int M_MAX = 100;
const int N_MAX = 100;
int dp(vector >&grid, int m, int n) {
int mat[M_MAX+2][N_MAX+2] = {0};
mat[m][n+1] = 1;
for (int r = m; r >= 1; r--)
for (int c = n; c >= 1; c--){
int t = 0;
if (grid[r+1][c] == 0)
t = t+mat[r+1][c];

if (grid[r][c+1] == 0)
t = t+mat[r][c+1];

mat[r][c] = t;
}
return mat[1][1];
}
c*******r
发帖数: 610
2
OJ 上面试一试就知道了
1 (共1页)
进入JobHunting版参与讨论
相关主题
Unique Path II【update】cs小硕 下周g onsite 求祝福
FB onsite 面经matrix question
感觉leetcode的OJ有点太偏重DP了继续分享G家phone interview题目
Leetcode和CC150上的Unique Paths问题求教请教leetcode上的minimum path sum有space O(M+N)的解法吗?
leetcode题目请教一个leetcode上的题
G家题讨论: harry potter 走矩阵攒人品,google电话面经
leetcode里, backtracking的time complexity怎么算,比如permutations这题目twitter电面
enumerate all unique paths of robot问个计算化学问题:怎么读GRID?
相关话题的讨论汇总
话题: grid话题: int话题: mat话题: max话题: backtrack