Leetcode 动态规划 Unique Paths
本文为senlie原创。转载请保留此地址:http://blog.csdn.net/zhengsenlie
Unique Paths
Total Accepted: 17915 Total
Submissions: 57061My Submissions
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?
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
题意:给定一个 m * n 的网格,一个机器人要从左上角走到右下角,每次仅仅能向下或向右移动一个位置。
问有多少种走法
思路1:dfs暴力枚举
复杂度:超时了... O(2^n)
思路2:记忆化搜索
用一个数组paths[i][j]记录从 (0,0) 到 (m,n)的路径数
思路3:dp
设置状态为f[i][j],表示从(0,0)到达网格(i,j)的路径数,则状态转移方程为
f[i][j] = f[i - 1][j] + f[i][j - 1]
复杂度:时间O(n^2) 空间 O(n)
<pre name="code" class="cpp">//思路1
int uniquePaths(int m, int n){
if(m < 0 || n < 0) return 0;
if(m == 1 && n == 1) return 1;
return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);
} //思路2
//paths[i][j]表示从(0,0)到(i,j)的路径数
int paths[101][101];
int dfs(int m, int n){
if(m < 0 || n < 0) return 0;
if(m == 1 && n == 1) return 1;
if(paths[m][n] >= 0) return paths[m][n];
return paths[m][n] = dfs(m - 1, n) + dfs(m, n - 1);
}
int uniquePaths(int m, int n){
memset(paths, -1, sizeof(paths));
return dfs(m, n);
} //思路2还有一种写法
//paths[i][j]表示从(i,j)到(m - 1,n - 1)的路径数
int paths[101][101];
int mm, nn;
int dfs(int x, int y){
if(x >= mm || y >= nn) return 0;
if(x == mm - 1 && y == nn - 1) return 1;
if(paths[x][y] >= 0) return paths[x][y];
return paths[x][y] = dfs(x + 1, y) + dfs(x, y + 1);
}
int uniquePaths(int m, int n){
mm = m, nn = n;
memset(paths, -1, sizeof(paths));
return dfs(0, 0);
} //思路3 paths[i][j] 表示(0, 0) 到(i,j)的路径数
int paths[101][101];
int uniquePaths(int m, int n){
memset(paths, 0, sizeof(paths));
for(int i = 0; i < m; ++i) paths[i][0] = 1;
for(int j = 0; j < n; ++j) paths[0][j] = 1;
for(int i = 1 ; i < m; ++i){
for(int j = 1; j < n; ++j){
paths[i][j] = paths[i - 1][j] + paths[i][j - 1];
}
}
return paths[m - 1][n - 1];
}
思路3 还有一种写法
用一个一维数组 paths[j] 表示 (0, 0) 至 (i, j)的路径数,在外循环变量为 i 时,还没更新前
paths[j] 相应上面二维数组写法的paths[i - 1, j],paths[j - 1]相应paths[i][j - 1]
int paths[101];
int uniquePaths(int m, int n){
memset(paths, 0, sizeof(paths));
paths[0] = 1;
for(int i = 0; i < m; ++i){
for(int j = 1; j < n; ++j){
paths[j] = paths[j] + paths[j - 1];
}
}
return paths[n - 1];
}
Leetcode 动态规划 Unique Paths的更多相关文章
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- [LeetCode] 62. Unique Paths 唯一路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- [LeetCode] 63. Unique Paths II 不同的路径之二
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 62. Unique Paths 不同的路径
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【leetcode】Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode 之 Unique Paths
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagra ...
- LeetCode 62. Unique Paths(所有不同的路径)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
随机推荐
- Cookie已经过时,细看Facebook, Google, Apple如何追踪用户
http://www.infoq.com/cn/news/2014/10/cookie-facebook-google-apple 链接地址 Cookie,有时也用其复数形式Cookies,指某些网站 ...
- Excel如何进行SVN
KSFramework常见问题:Excel如何进行SVN协作.差异比较? Excel如何进行SVN协作.差异比较? 嗯,这是一个令人困惑的问题.游戏开发.程序开发时,使用Excel可以添加文档.注 ...
- 服务器是R710常见错误汇总:
报错: E1422 CPU 1 machine check error . power cycle AC 解决方案: 系统 BIOS 已报告机器检查错误.请断开系统的交流电源 10 秒,然后重新启动系 ...
- Git-常用命令集合
该文章会陆续添加内容,学习网页来自http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 ...
- lock table
1.在执行lock table语句后,则在执行unlock tables之前,当前会话只能操作当前被锁定的表(包括表别名)2.read锁,其它会话只有读取权限,没有写入权限3.write锁,其它会话只 ...
- SMT实用工艺
第一章 SMT概述 SMT(表面组装技术)是新一代电子组装技术.经过20世纪80年代和90年代的迅速发展,已进入成熟期.SMT已经成为一个涉及面广,内容丰富,跨多学科的综合性高新技术.最新几年,SMT ...
- [js - 算法可视化] 汉诺塔(Hanoi)演示程序
前段时间偶然看到有个日本人很早之前写了js的多种排序程序,使用js+html实现的排序动画,效果非常好. 受此启发,我决定写几个js的算法动画,第一个就用汉诺塔. 演示地址:http://tut.ap ...
- centos 修改shm
Linux下,Oracle 11g的自动内存管理不能指定大于这个/dev/shm的总量内存.否则就会出现如下错误 ORA-00845: MEMORY_TARGET not supported on t ...
- java常用日期函数总结
请记得要引入java.util.Date和java.text.SimpleDateFormat两个包 1.计算某一月份的最大天数 Calendar time=Calendar.getInstance( ...
- c# 获取全屏 中鼠标焦点的位置坐标
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...