8.2 Robot in a Grid
来源:互联网 发布:淘宝客设置推广位 编辑:程序博客网 时间:2024/06/05 19:04
Simple dynamic problem, we can reduce the memory space we used by only using O(n) space instead of O(mn) space.
O(mn) space code:
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { // write your code here int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector<vector<int> > dp(m+1,vector<int>(n+1,0)); dp[0][1] = 1; for(int i = 1; i <= m; ++i){ for(int j = 1; j <= n; ++j){ if(!obstacleGrid[i-1][j-1]) dp[i][j] = dp[i-1][j]+dp[i][j-1]; } } return dp[m][n]; }
O(n) space;
int uniquePathsWithObstacles(vector<vector<int> > &obstacleGrid) { int m = obstacleGrid.size(); int n = obstacleGrid[0].size(); vector<int> dp(n,0); for(int j = 0; j<n; ++j){ if(obstacleGrid[0][j] != 1) dp[j] = 1; else break;// if there is a 1: which means the grid after it cannot be reach at initialization time. } for(int i = 1; i<m; ++i){ dp[0] = (obstacleGrid[i][0] == 1) ? 0 : dp[0]; for(int j = 1; j<n; ++j){ dp[j] = (obstacleGrid[i][j] == 1) ? 0 : dp[j] + dp[j-1]; } } return dp[n-1]; }
0 0
- 8.2 Robot in a Grid
- Best Route in a Grid
- Problem 11:Largest product in a grid
- (advanced) UVA 10949Kids in a Grid
- PE 11 Largest product in a grid
- Hackerrank Connected Cell in a Grid
- hihocoder1241 : Best Route in a Grid
- hihocoder 1241:Best Route in a Grid
- HIHOCODE #1241:Best Route in a Grid
- hihoCoder 1241 Best Route in a Grid
- Connected Cells in a Grid(dfs)
- Problem 11 Largest product in a grid
- Creating a Details Popup Window Grid in a DataGrid
- A survey on the communication architectures in smart grid(1)
- A survey on the communication architectures in smart grid(2)
- Largest product in a grid( Project Euler problem 11)
- Project Euler 题解 #11 Largest product in a grid
- project euler problem 11:Largest product in a grid
- LeetCode 题解(281) :Binary Tree Longest Consecutive Sequence
- 黑马程序员--面向对象的特征(继承、多态、内部类等)
- 国企离职!到了放手的时候!——北漂18年(30)
- 使用androidstudio自带的git插件进行版本控制图文详解
- 8.1 Triple Stair
- 8.2 Robot in a Grid
- #ifdef __cplusplus extern "C" { #endif 的解释
- ubuntu关机重启命令介绍
- #define GPBCON (*(volatile unsigned *)0x56000010) 的理解:
- 题解
- BZOJ 3124 [Sdoi2013]直径 DFS
- 4.4.6 切换全屏模式
- adb使用大全
- 信息安全推荐书籍