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: 画图自己做这个题.^^

主要是先建模型,既,分析出:

  1. 状态转移公式,本题有3个状态转移公式,分别对应这i,j条件.具体参考最下方的代码(简单递归,当然无法执行,因为时间复杂度为 \(O(2^n)\), 但能很好的展示了转移公式);
  2. 最优子结构(分别对应各自的状态转移公式);
  3. 边界,既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);
}
};

随机推荐

  1. 新概念英语(1-121)The man in a hat

    Why didn't Caroline recognize the customer straight away ?A:I bought two expensive dictionaries here ...

  2. 微信小程序开发-IP地址查询-例子

    微信小程序开发  小程序搜索框  IP地址查询  搜索查询  样例 微信小程序 开发 参考   https://mp.weixin.qq.com/debug/wxadoc/dev/component/ ...

  3. [js]关于call()和apply()的理解

    call 和 apply 都是为了改变某个函数运行时的 context 即上下文而存在的,换句话说,就是为了改变函数体内部 this 的指向. 因为 JavaScript 的函数存在「定义时上下文」和 ...

  4. MongoDB 分片集群搭建

    一.概述 分片是一种在多台机器上分配数据的方法.MongoDB使用分片来支持具有非常大的数据集和高吞吐量操作.有两种解决系统增长的方法:垂直扩展和水平扩展. 垂直扩展涉及增加单个服务器的容量,例如使用 ...

  5. Linux OpenGL 实践篇-1 OpenGL环境搭建

    本次实践所使用环境为CentOS 7. 参考:http://www.xuebuyuan.com/1472808.html OpenGL开发环境搭建: 1.opengl库安装 opengl库使用mesa ...

  6. [LeetCode] Find Permutation 找全排列

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  7. MySQL之存储过程和函数

    存储过程和函数: 1.创建存储过程和函数: 存储过程: delimiter $$ create procedure proc_name() BEGIN 查询语句; // 记得加分号 END $$ de ...

  8. [HNOI2016]大数

    题目描述 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个询问求 S 的 ...

  9. 【BZOJ2186】【SDOI2008】沙拉公主的困惑

    Description ​ 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行编号与M!互质的钞票.房地产第一大户沙拉公主决定预测一下大富翁 ...

  10. 计蒜客NOIP2017提高组模拟赛(五)day1-展览

    传送门 发现这题选或不选对状态的优劣程度不会产生影响,如果已经确定了两个数a和b,那么最优的首项和公比也都是唯一确定的, 与对于后面的数x,加进去也好不加进去也好,首项和公比依旧是原来的 于是我们用尺 ...