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” ). 机器人每次只能向下或者向 ...
随机推荐
- window下安装composer and yii2
我的环境是集合包xampp 1,下载composer:下载地址https://getcomposer.org/download/, 点击蓝色字体“Composer-Setup.exe” 2,安装com ...
- 一个简单的将GUI程序的log信息输出到关联的Console窗口中(AllocConsole SetConsoleTitle WriteConsole 最后用ShowWindow(GetConsoleWindow)进行显示)
// .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...
- C#基础精华08(反射,程序集)
什么是程序集? 程序集是.net中的概念. .net中的dll与exe文件都是程序集.(exe与dll的区别?) 程序集(Assembly),可以看做是一堆相关类打一个包,相当于java中的jar包( ...
- 8、双向一对多的关联关系(等同于双向多对一。1的一方有对n的一方的集合的引用,同时n的一方有对1的一方的引用)
双向一对多关联关系 “双向一对多关联关系”等同于“双向多对一关联关系”:1的一方有对n的一方的集合的引用,同时n的一方有对1的一方的引用. 还是用客户Customer和订单Order来解释: “一对多 ...
- Windows JAVA 环境配置
Java SE Development Kit Downloads http://www.oracle.com/technetwork/java/javase/overview/index.html ...
- 设置COOKIE过期时间的方法
第一,日期运算法 1)将期限设置为当前日期后的第N天的0时0分0秒 Response.Cookies(LastView).Expires=dateadd(d,N,date) 2)将期限设置为当前日期后 ...
- Building Xcode iOS projects and creating *.ipa file from the command line
For our development process of iOS applications, we are using Jenkins set up on the Mac Mini Server, ...
- C#计算程序执行速度
long t1 = DateTime.Now.Ticks; //执行程序,例如处理100个文件 long t2 = DateTime.Now.Ticks; Response.Write("执 ...
- 待实践三:MVC3下 路由的测试 使用 RouteDebug.dll 来测试判断路由是否符合
在需要进行测试路由是否匹配的项目中引用 RouteDebug.dll 并且在MVC的Global.asax里面加入一段代码 //下面这行代码一定是在 RegisterRoutes(Rou ...
- Java程序员常用工具集
我发现很多人没办法高效地解决问题的关键原因是不熟悉工具,不熟悉工具也还罢了,甚至还不知道怎么去找工具,这个问题就大条了.我想列下我能想到的一个Java程序员会用到的常用工具. 一.编码工具 1.IDE ...