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);
}
};
随机推荐
- SpringCloud的Bus(一)消息中间件的概念和用途
一.概念与定义 1.Message Broker Message Broker是一种消息验证.消息转换.消息路由的架构模式,用于如: 消息路由到一个或多个目的地 消息转化为其他的表现方式 执行消息的聚 ...
- Linux--慕课学习
刚开始接触Linux,很有幸的在慕课网上看到了Peter老师的Linux入门课程,老师讲课真的式行云流水,深入浅出,循循善诱,层层递进. 老师分享的都是自己多年来总结的经验.看完之后也学到了很多东西. ...
- Dijkstra的双栈算术表达式求值算法
这次来复习一下Dijkstra的双栈算术表达式求值算法,其实这就是一个计算器的实现,但是这里用到了不一样的算法,同时复习了栈. 主体思想就是将每次输入的字符和数字分别存储在两个栈中.每遇到一个单次结束 ...
- python jquery
jquery 一.寻找元素(选择器和筛选器) a.选择器 1.基本选择器 1 $("*") $("#id") $(".class") ...
- POJ-2993 Emag eht htiw Em Pleh---棋盘模拟
题目链接: https://vjudge.net/problem/POJ-2993 题目大意: 输入和输出和这里相反. 思路: 模拟题,没啥算法,直接模拟,不过为了代码精简,还是花了一点心思的 #in ...
- sql serve 数据库游标的使用
什么是游标? 通俗来讲,个人理解,游标是对一个查询结果集,每次取出一条数据进行处理操作. 使用场景: 例如,我们要修改一个表300条数据,且每条数据修改的内容不一样,那么平时用的update ...
- [LeetCode] Find K-th Smallest Pair Distance 找第K小的数对儿距离
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...
- [LeetCode] Employee Free Time 职员的空闲时间
We are given a list schedule of employees, which represents the working time for each employee. Each ...
- 【django小练习之主机管理界面】
需求: 利用django,js,bootstrap等实现登录,主机管理等操作. 实现截图 登录界面 主机界面,添加及编辑 部门管理界面 代码实现 目录层级 settings.py "&quo ...
- kafka知识体系-kafka设计和原理分析-kafka leader选举
kafka leader选举 一条消息只有被ISR中的所有follower都从leader复制过去才会被认为已提交.这样就避免了部分数据被写进了leader,还没来得及被任何follower复制就宕机 ...