【Unique Paths】cpp
题目:
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.
代码:
class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n];
memset(dp, , sizeof(dp));
for ( size_t i = ; i < n; ++i ) dp[][i] = ;
for ( size_t i = ; i < m; ++i ) dp[i][] = ;
for ( size_t i = ; i < m; ++i )
{
for ( size_t j = ; j < n; ++j )
{
dp[i][j] = dp[i-][j] + dp[i][j-];
}
}
return dp[m-][n-];
}
};
tips:
常规dp解法。
=====================================
上面的代码有可以改进的地方:dp[m][n]并不用这些额外空间,只需要两个长度为n的数组即可;一个保存前一行的状态,一个用于遍历当前行的状态,每次滚动更新,可以省去额外空间。沿着上述思路改进了一版代码如下:
class Solution {
public:
int uniquePaths(int m, int n) {
int curr[n], pre[n];
for ( size_t i = ; i<n; ++i ) { pre[i]=; curr[i]=; }
curr[] = ;
for ( size_t i = ; i<m; ++i )
{
for ( size_t j = ; j<n; ++j )
{
curr[j] = curr[j-] + pre[j];
pre[j] = curr[j];
}
curr[] = ;
}
return pre[n-];
}
};
这个代码空间复杂度降到了O(n),但还是可以改进。其实只用一个一维的数组dp就可以了,代码如下。
class Solution {
public:
int uniquePaths(int m, int n) {
int curr[n];
memset(curr, , sizeof(curr));
curr[] = ;
for ( size_t i = ; i < m; ++i )
{
for ( size_t j = ; j < n; ++j )
{
curr[j] = curr[j-] + curr[j];
}
}
return curr[n-];
}
};
这里用到了滚动数组的技巧。有个细节需要注意,外层dp是可以从0行开始,省去了一部分代码。
=====================================
再学一种深搜+缓存(即“备忘录”)解法。
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > cache(m+,vector<int>(n+,));
return Solution::dfs(m, n, cache);
}
static int dfs( int x, int y, vector<vector<int> >& cache )
{
if ( x< || y< ) return ;
if ( x== && y== ) return ;
int left = cache[x-][y]> ? cache[x-][y] : cache[x-][y]=Solution::dfs(x-, y, cache);
int up = cache[x][y-]> ? cache[x][y-] : cache[x][y-]=Solution::dfs(x, y-, cache);
return left+up;
}
};
有点儿类似动态规划的思想:开一个cache数组保存已经深搜遍历过的中间结果,避免重复遍历。
这里有个简化代码的技巧:定义cache的时候多定义一行和一列,这样在深搜的过程中按照dfs中的代码可以省去判断cache下标是否越界的逻辑。
===============================================
第二次过这道题,直接写了一个dp的做法。
class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n];
fill_n(&dp[][], m*n, );
for ( int i=; i<n; ++i ) dp[][i]=;
for ( int i=; i<m; ++i ) dp[i][]=;
for ( int i=; i<m; ++i )
{
for ( int j=; j<n; ++j )
{
dp[i][j] = dp[i][j-] + dp[i-][j];
}
}
return dp[m-][n-];
}
};
【Unique Paths】cpp的更多相关文章
- leetcode 【 Unique Paths 】python 实现
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- 【Unique Paths II】cpp
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- leetcode 【 Unique Paths II 】 python 实现
题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- Hdu 4734 【数位DP】.cpp
题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...
- 【Minimum Window】cpp
题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...
- 【Sudoku Solver】cpp
题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated b ...
- 【Combination Sum 】cpp
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C ...
随机推荐
- DB2数据库备份还原
恢复及备份NC DB2数据库步 一. 安装DB2数据库 解压db2v9.5ins.rar安装,在写此文档时客户一般用的是9.5: 注意不要将db2安装到系统盘: 二. Windows版本 1.数据库备 ...
- Python爬虫实战:爬糗事百科的段子
一个偶然的机会接触了Python,感觉很好用,但是一直在看c++啥的,也没系统学习.用过之后也荒废了许久.之前想建个公众号自动爬糗事百科的段子,但是没能建起来,真是尴尬,代码上传的服务器上之后,不能正 ...
- java常用框架总结
一.SpringMVC http://blog.csdn.net/evankaka/article/details/45501811 Spring Web MVC是一种基于Java的实现了Web MV ...
- iptables (2) 基本配置
iptables 基本命令使用举例 一.链的基本操作 1.清除所有的规则.1)清除预设表filter中所有规则链中的规则.# iptables -F -F, --flush [chain] Flush ...
- ORACLE的raw属性
网上说RAW类型在网络数据传送的时候可以避免字节的字符集转换,在mssql中使用的GUID类型在oracle中对应的也是raw类型(一般是raw(16)),如果此时使用连接查询将raw类型的字段和va ...
- POJ 3187 Backward Digit Sums (递推,bruteforce)
第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来 然后next_permutation直接暴. #include<cstdio> #include<iostream&g ...
- 【BZOJ4571】[SCOI2016] 美味(主席树)
点此看题面 大致题意: 给你一个序列\(a\),然后每次询问\(max_{i=l}^r(a_i+x)\ xor\ b\). 大致思路 首先,我们要知道一个简单的性质:位运算时位与位之间是互不影响的. ...
- 【BZOJ2427】[HAOI2010] 软件安装(缩点+树形DP)
点此看题面 大致题意: 有\(N\)个软件,每个软件有至多一个依赖以及一个所占空间大小\(W_i\),只有当一个软件的直接依赖和所有的间接依赖都安装了,它才能正常工作并造成\(V_i\)的价值.求在容 ...
- CPP-基础:C_C++变量命名规则
C_C++变量命名规则 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++必须遵守的变量命名规则: 1. 变量名只能是字母(A-Z,a-z)和数字(0-9)或者下划线(_)组成. 2. 第一 ...
- Linux环境下使用xampp配置php开发环境
XAMPP (Apache+MySQL+PHP+PERL)是一个功能强大的建站集成软件包.这个软件包原来的名字是LAMPP,但是为 了避免误 解,最新的几个版本就改名为 XAMPP 了.它可以在Win ...