当前位置:网站首页>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];
}
}
边栏推荐
- Unity backpack system
- 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
- CH340 RTS DTR引脚编程驱动OLED
- Responsive high-end website template source code Gallery material resource download platform source code
- The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
- Build an "industrial brain" and improve the park's operation, management and service capabilities with "digitalization"!
- Prefix-Tuning: Optimizing Continuous Prompts for Generation
- Ch340 RTS DTR pin programming drives OLED
- The wonderful use of asemi rectifier bridge GBPC3510 in DC brush motor
猜你喜欢

Volvo: what on earth does the deep-rooted "sense of security" rely on?

数据挖掘-01

Responsive high-end website template source code Gallery material resource download platform source code

conda虚拟环境总结与解读

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

leetcode刷题:动态规划08(分割等和子集)

MySQL Basics (create, manage, add, delete, and modify tables)

【原型与原型链】初识原型与原型链~

Illustrated: detailed explanation of JVM memory layout

【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
随机推荐
Tensorboard usage record
LightPicture – 精致图床系统
AI首席架构师12-AICA-百度OCR垂类规模化落地实践
搬家通知!
Ch340 RTS DTR pin programming drives OLED
Jumping game II in question 45 of C language power deduction. Ergodic jump
【P4】 查看库文件两个历史版本的区别
How to solve MySQL deep paging problem
Server memory failure prediction can actually do this!
高等数学(第七版)同济大学 习题3-4 个人解答(前8题)
verticle-align行内元素垂直居中对齐
贪心——122. 买卖股票的最佳时机 II
动态规划——509. 斐波那契数
动态规划——62. 不同路径
TypeError: ufunc ‘bitwise_ and‘ not supported for the input types, and the inputs could not be safely
服务器内存故障预测居然可以这样做!
Super nice PHP program source code of nteam official website
LeetCode 0141. 环形链表 - 三种方法解决
[wrong question]
Leetcode brush question: dynamic planning 09 (weight of the last stone II)