62. Unique Paths(中等,我自己解出的第一道 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?
有个图来着,Markdown贴起来很麻烦.不贴了.
Above is a 3 x 7 grid. How many possible unique paths are there?
Note: \(m\) and \(n\) will be at most 100.
解题思路,没啥可说, 用 dynamic programming.
步骤:
step 1: 看 https://mp.weixin.qq.com/s/0AgJmQNYAKzVOyigXiKQhA, 动态规划入门最好材料,没有之一! 10分钟看完,然后第二步.
step 2: 画图自己做这个题.^^
主要是先建模型,既,分析出:
- 状态转移公式,本题有3个状态转移公式,分别对应这i,j条件.具体参考最下方的代码(简单递归,当然无法执行,因为时间复杂度为 \(O(2^n)\), 但能很好的展示了转移公式);
- 最优子结构(分别对应各自的状态转移公式);
- 边界,既
F[0,0] = 1.
自己想法,自个代码^^:
\(O(m*n)\) time, \(O(n)\) extra space.
class Solution {
public:
// 真正的DP求解
// $O(m*n)$ time, $O(n)$ extra space.
// 时间复杂度无法再小了,但空间复杂度还可以再小.
// space complexity 最低可为 min(m, n);
// 听说有 $O(1)$ 的空间复杂度?
// 如果不用 DP, 可用 math 的方法,如下:
// https://leetcode.com/problems/unique-paths/discuss/
int uniquePaths(int m, int n) {
if(m == 1 || n == 1) return 1;
if(m < n) return uniquePaths(n, m);
vector<int> temp(n - 1, 0);
for(int i = 1; i < m; i++){
for(int j = 1; j < n; j++){
if(i == 1 && j == 1) temp[0] = 2;
else if(i == 1 && j > 1) temp[j - 1] = 1 + temp[j - 2];
else if(i > 1 && j == 1) temp[0] = temp[0] + 1;
else if(i > 1 && j > 1) temp[j - 1] = temp[j - 1] + temp[j - 2];
}
}
return temp[n - 2];
}
};
下面是简单递归,但 time complexity \(O(2^n)\), 太高了.
下面代码可清晰地展示出DP的三要素:
状态转移公式、最优子结构和边界.
class Solution {
public:
// 方法一: 简单递归,但 time complexity $O(2^n)$, 太高了.
int uniquePaths(int m, int n) {
int i = m - 1, j = n - 1;
if(i == 0 && j == 0) return 1;
else if(i == 0 && j != 0) return uniquePaths(i, j - 1);
else if(j == 0 && i != 0) return uniquePaths(i - 1, j);
else if(j > 0 && i > 0) return uniquePaths(i, j - 1) + uniquePaths(i - 1, j);
}
};
随机推荐
- Linux CentOS7.0 (03)安装验证 docker
一.安装docker 1.升级 Linux 的软件包和内核 sudo yum update 2.安装 docker (1) sudo yum install docker (2).验证docker安 ...
- linux下xargs和管道的区别
管道将前面的标准输出作为后面的标准输入,xargs则将标准输入作为命令的参数 一.简介 1.背景 之所以能用到这个命令,关键是由于很多命令不支持|管道来传递参数,而日常工作中有有这个必要,所以就有了x ...
- OAuth2.0学习(1-12)开源的OAuth2.0项目和比较
OAuth2.0学习(2-1)OAuth的开源项目 1.开源项目列表 http://www.oschina.net/project/tag/307/oauth?lang=19&sort=t ...
- logback中配置的日志文件的生成地址
配置文件如下 <?xml version="1.0" encoding="UTF-8"?> <configuration debug=&quo ...
- Linux-centos-7.2-64bit 安装配置mysql
2018-04-12 安装在/usr/local/下,配置文件在/etc/my.ini 1.下载mysql安装包到 /usr/local/software cd /usr/local/software ...
- Django(博客系统):基于pycharm如何一个django工程下创建多个app
背景:通常我们创建一个django系统时,为了把业务模块划分清楚往往会把一个独立的业务模块放到一个app中,如果多个独立的业务模块就会创建多个app,一般情况下为了更好的管理这些app,会把他们都存放 ...
- React-Native(二):React Native开发工具vs code配置
从网上翻阅了一些开发react-native的开发工具时,发现其实可选的工具还是比较多的Sublime Text,WebStrom,Atom+Nuclide,vs code 等.因为我用.net生态环 ...
- Java面试题—初级(3)
21.ArrayList和Vector的区别 这两个类都实现了List接口(List接口继承了Collection接口),他们都是有序集合,即存储在这两个集合中的元素的位置都是有顺序的,相当于一种动态 ...
- html<!DOCTYPE>声明标签
html<!DOCTYPE>声明标签 <DOCTYPE>声明是html文档的第一行,位于<html>标签之前 <DOCTYPE>声明不是html标签,他 ...
- Extensions in UWP Community Toolkit - WebViewExtensions
概述 UWP Community Toolkit Extensions 中有一个为 WebView 提供的扩展 - WebViewExtensions,本篇我们结合代码详细讲解 WebView Ext ...