LeetCode题解——Unique Path(DP与优化)
题目: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?
题意:在m*n的网格中,从左下角一步步走到右上角,有多少种可能的走法(每次只能横或竖移动一步)
在第一象限的话,也就是每次走一步从(0,0)走到(m,n)有多少种走法
思路:考虑这是一个递推的问题,根据DP思想,有递推公式
我的代码比较短,因为memset只能置0或者-1,可以把数组置为-1,然后和取负就是所求结果了。
class Solution {
public:
int uniquePaths(int m, int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int f[m][n];
memset(f, -, sizeof(int) * m * n); //数组全部置-1
for (int i = ; i < m; i++) { //求和
for (int j = ; j < n; j++) {
f[i][j] = f[i - ][j] + f[i][j - ];
}
}
return -f[m - ][n - ]; //取负
}
};
时间复杂度O(m*n),那么可不可以继续优化呢?
上面采用的是二维数组,现在可以用一位数组取代之,则
Fn=Fn-1+Fn;
class Solution {
public:
int uniquePaths(int m, int n) {
vector<int> vec(n, );
for(int i=; i<m; ++i){
for(int j=; j<n; ++j){
vec[j]+=vec[j-];
}
}
return vec[n-];
}
};
LeetCode题解——Unique Path(DP与优化)的更多相关文章
- LeetCode 63. Unique Path II(所有不同路径之二)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode 题解]:Path Sum
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a bi ...
- 【leetcode】 Unique Path ||(easy)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [LeetCode]题解:005-Longest Palindromic Substring优化
题目来源和题意分析: 详情请看我的博客:http://www.cnblogs.com/chruny/p/4791078.html 题目思路: 我上一篇博客解决这个问题的时间复杂度是最坏情况是(O(n^ ...
- 【bzoj1097】[POI2007]旅游景点atr 状压dp+堆优化Dijkstra
题目描述 FGD想从成都去上海旅游.在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣的事情.经过这些城市的顺序不是完全随意的,比如说FGD不希望在刚吃过一顿大餐之后立刻去下一个 ...
- [LeetCode]题解(python):062 Unique path
题目来源 https://leetcode.com/problems/unique-paths/ A robot is located at the top-left corner of a m x ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)
Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...
随机推荐
- java io异步
1.一般来说,可以通过多线程的方式来实现异步 2.同步和异步着重点在于多个任务的执行过程中,一个任务的执行是否会导致整个流程的暂时等待: 3.而阻塞和非阻塞着重点在于发出一个请求操作时,如果进行操作的 ...
- iOS开发--数组
1.sortedArrayUsingSelector (按Key值大小对NSDictionary排序) NSMutableArray *array = [NSMutableArray arrayWit ...
- iOS 使用COPY声明NSSTRING属性
使用COPY声明NSSTRING属性 2014/05/29 JACE 发表回复 声明一个NSString属性使用copy要优于使用strong.这同样适用于遵守NSCoding协议的不可变类(immu ...
- Android开发之EditText属性详解
1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: 把该EditText设为:android:password="true" // 以”.”形式显示文本 ( ...
- git push提示或错误
当 git 和 gerrit 一起使用的时候,你创建了一个 tag,现在需要 push 到远程仓库,当你没有权限的时候,会出现如下提示: $ git push origin v20150203 Tot ...
- 1450. Russian Pipelines(spfa)
1450 水题 最长路 #include <iostream> #include<cstdio> #include<cstring> #include<alg ...
- Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)
题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出 ...
- axis : java.lang.NoSuchMethodError
Hi friends,Iam getting the following error when deploying my app in jboss error.Iam new to axis .can ...
- hdu3037 Lucas定理
Lucas定理 Lucas(n,m,p)=c(n%p,m%p)* Lucas(n/p,m/p,p),其中lucas(n,m,p)=C(n,m)%p (这里的除号是整除) 证明——百度百科 题意:求n个 ...
- HDU 4864 (2014 Multi-University Training Contest 1 )
考试时,想到了一个很类似的方法,但是总是差那么点,就是这么点,需要不断的努力啊!!! 题解: 基本思想是贪心. 对于价值c=500*xi+2*yi,yi最大影响100*2<500,所以就是求xi ...