当前位置:网站首页>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];
}
}
边栏推荐
- [openvx] VX for basic use of objects_ convolution
- leetcode刷题:动态规划08(分割等和子集)
- ES6 from entry to mastery 07: Deconstruction assignment
- MySQL Basics (create, manage, add, delete, and modify tables)
- 4-day excel practical training camp, 0.01 yuan special offer for only three days, 200 sets of learning kits
- "Xiaodeng" network equipment monitoring in operation and maintenance
- 动态规划——62. 不同路径
- Swift中的协议
- Unity simply implements the dialog function
- The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
猜你喜欢

WordPress simple mkblog blog theme template v2.1

Leetcode brush question: dynamic planning 09 (weight of the last stone II)

After 95, Alibaba P7 published the payroll: it's really heartbreaking

Tensorboard usage record

STM32 RT thread virtual file system mount operation

构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!

Prefix-Tuning: Optimizing Continuous Prompts for Generation

Outlook tutorial, how to use color categories and reminders in outlook?

The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor

Data mining-02
随机推荐
《剑指offer》| 刷题小记
搬家通知!
TypeError: ufunc ‘bitwise_ and‘ not supported for the input types, and the inputs could not be safely
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning
[openvx] VX for basic use of objects_ distribution
LeetCode 0141. 环形链表 - 三种方法解决
Digital economy has become the biggest attraction
Differences among BRD, MRD and PRD
我的创作纪念日
最新版宝塔安装zip扩展,php -m 不显示的处理方法
Qt:qmessagebox message box, custom signal and slot
一篇文章掌握Postgresql中对于日期类数据的计算和处理
2022 summary of the latest Android handler related interview questions
【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
数据挖掘-02
Prefix-Tuning: Optimizing Continuous Prompts for Generation
Mouse operation and response
C语言:不创建临时变量实现两数交换
leetcode刷题:动态规划08(分割等和子集)
动态规划——1049. 最后一块石头的重量 II