当前位置:网站首页>Dynamic planning - 62. Different paths
Dynamic planning - 62. Different paths
2022-07-28 03:44:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
A robot is in a m x n The top left corner of the grid ( The starting point is marked as “Start” ).
The robot can only move down or right one step at a time . The robot tries to reach the bottom right corner of the grid ( In the figure below, it is marked as “Finish” ).
Ask how many different paths there are in total ?
source : Power button (LeetCode)
link :https://leetcode.cn/problems/unique-paths
2 Title Example

Example 2:
Input :m = 3, n = 2
Output :3
explain :
From the top left corner , All in all 3 Path to the bottom right .
- towards the right -> Down -> Down
- Down -> Down -> towards the right
- Down -> towards the right -> Down
Example 3:
Input :m = 7, n = 3
Output :28
Example 4:
Input :m = 3, n = 3
Output :6
3 Topic tips
1 <= m, n <= 100
Question data guarantee that the answer is less than or equal to 2 * 109
4 Ideas
We use it f(i,j) It means walking from the upper left corner to (i,j) Number of paths for , among i and j The scope of each is [0, m) and [0, n).
Because we can only move down or right with each step — Step , So if you want to go to (i,j), If you take a step down , So from (i-1,j) Come over ; If you take a step to the right , So from (i,j-1) Come over . So we can write the dynamic programming transfer equation :
It should be noted that , If i=0, that f(i - 1,j) It is not a state that meets the requirements , We need to ignore this ― term ; Empathy , If j=0, that f(i,j-1) It is not a state that meets the requirements , We need to ignore this — term . The initial condition is f(0,0)= 1, That is, from the upper left corner to the upper left corner — Methods . The final answer is f(m - 1,n - 1).
details :
In order to facilitate code writing , We can put all f(0,j) as well as f(i,0) Are set as boundary conditions , Their values are 1.
Complexity analysis
Time complexity :O(mn).
Spatial complexity :O(mn), That is, the space required to store all States . be aware f(i,j) Only with section i Xing He i-1 The status of the line is related to , Therefore, we can use the rolling array instead of the two-dimensional array in the code , Reduce the space complexity to O(n). Besides , Since we exchange the values of rows and columns, it will not affect the answer , So we can always exchange m and n bring m≤ n, In this way, the spatial complexity is reduced to O(min(m, rn)).
5 My answer
class Solution {
public int uniquePaths(int m, int n) {
int[][] f = new int[m][n];
for (int i = 0; i < m; ++i) {
f[i][0] = 1;
}
for (int j = 0; j < n; ++j) {
f[0][j] = 1;
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
}
return f[m - 1][n - 1];
}
}
边栏推荐
- 动态规划——62. 不同路径
- 【OPENVX】对象基本使用之vx_lut
- 服务器内存故障预测居然可以这样做!
- MSGAN用于多种图像合成的模式搜索生成对抗网络---解决模式崩塌问题
- 贪心——122. 买卖股票的最佳时机 II
- 12月份PMP考试首次采用新考纲,该怎么学?
- LeetCode 0140. 单词拆分 II
- 8000 word explanation of OBSA principle and application practice
- Recursion and non recursion are used to calculate the nth Fibonacci number respectively
- C语言力扣第45题之跳跃游戏 II。遍历跳跃
猜你喜欢

MSGAN用于多种图像合成的模式搜索生成对抗网络---解决模式崩塌问题

verticle-align行内元素垂直居中对齐

Xctf attack and defense world web master advanced area php2

动态规划——63. 不同路径 II

数据挖掘-01

Leetcode skimming: dynamic programming 08 (segmentation and subsets)

LabVIEW加载和使用树型控件项目中的定制符号

Qt:QMessageBox消息框、自定义信号和槽

服务器内存故障预测居然可以这样做!

SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe 试读版
随机推荐
最新版宝塔安装zip扩展,php -m 不显示的处理方法
Unity backpack system
Leetcode58. 最后一个单词的长度
Tensorboard usage record
贪心——53. 最大子数组和
LeetCode 0141. 环形链表 - 三种方法解决
MySQL Basics (create, manage, add, delete, and modify tables)
【OPENVX】对象基本使用之vx_distribution
数据挖掘-01
ES6 从入门到精通 # 09:Symbol 类型
D2dengine edible tutorial (4) -- draw text
Vertical align align the elements in the row are vertically centered
【OPENVX】对象基本使用之vx_convolution
8000 word explanation of OBSA principle and application practice
Msgan is used for pattern search of multiple image synthesis to generate confrontation Network -- to solve the problem of pattern collapse
pip-script.py‘ is not present Verifying transaction: failed
VBA reads the create document of SQL in batches to create tables
conda虚拟环境总结与解读
【论文笔记】基于深度学习的移动机器人自主导航实验平台
Prefix-Tuning: Optimizing Continuous Prompts for Generation