当前位置:网站首页>Leetcode skimming: dynamic planning 04 (different paths)
Leetcode skimming: dynamic planning 04 (different paths)
2022-07-23 17:41:00 【Taotao can't learn English】
62. Different paths
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 ?
Example 1:

- Input :m = 3, n = 7
- Output :28
Example 2:
- Input :m = 2, n = 3
- Output :3
explain : From the top left corner , All in all 3 Path to the bottom right .
- towards the right -> towards the right -> Down
- towards the right -> Down -> towards the right
- Down -> towards the right -> towards the right
Example 3:
- Input :m = 7, n = 3
- Output :28
Example 4:
- Input :m = 3, n = 3
- Output :6
Tips :
- 1 <= m, n <= 100
- Question data guarantee that the answer is less than or equal to 2 * 10^9
- According to the dimension of data , Create a one-dimensional or two-dimensional data area , This is a two-dimensional area , Just make a two-dimensional table
- Initialize good value , Because the first row and the first column only have one route , So the initialization value is 1
- analysis dp [i] The meaning of ,dp [ i ] For how many ways to walk in the current position , That is to add up all the walking methods from the left and all the walking methods from the top
- according to dp[i] How did you get here , Construct assignment equations , That is, the state transition equation
package com.programmercarl.dynamic;
/** * @ClassName UniquePaths * @Descriotion TODO * @Author nitaotao * @Date 2022/7/23 9:13 * @Version 1.0 * https://leetcode.cn/problems/unique-paths/ * 62. Different paths **/
public class UniquePaths {
public int uniquePaths(int m, int n) {
/** * dp[ i ] How many ways to get to this position */
int[][] dp = new int[m][n];
dp[0][0] = 0;
// Initialize the first line
for (int i = 0; i < n; i++) {
dp[0][i] = 1;
}
// Initialize the first column
for (int i = 1; i < m; i++) {
dp[i][0] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
// The position of each cell is moved from the left grid or the upper grid
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
}

边栏推荐
- [introduction series of redis] data types and related commands of redis
- 使用 Preparedstatement 选择和显示记录的 JDBC 程序
- SAP HANA数据库备份失败解决办法
- 单细胞论文记录(part19)--A comprehensive comparison on cell-type composition inference for ST data
- 数智化时代文旅遇新机?中国移动咪咕造 “元宇宙第一岛”
- LeetCode_455_分发饼干
- Software configuration | pychart download, installation, environment configuration and uninstall
- 职场3道坎:年薪30万、50万、100万
- 来自某学生的求助,干了,闲暇能帮就帮一把!
- Do you really understand the persistence mechanism of redis?
猜你喜欢

Food safety chocolate is also true or false? How much do you know about it

数智化时代文旅遇新机?中国移动咪咕造 “元宇宙第一岛”

Pymoo learning (2): Bi objective optimization problems with constraints
Do you dare to use BigDecimal without mastering these pits?

基于策略路由部署的网络多出口设计研究与实现

59. General knowledge of lightning safety

食品安全|听起来很健康的植物肉,是什么来头?

Leetcode question brushing record

可视化机房管理

Geometric parametric reconstruction
随机推荐
True title of Blue Bridge Cup: Card [easy to understand]
MySQL7种JOIN(图)
Mysql: MySQL problem that is not a MySQL problem
makefile 常用函数notdir、wildcard、patsubst
Pymoo learning (2): Bi objective optimization problems with constraints
LeetCode_ 455_ Distribute cookies
工業物聯網中的時序數據
Implementation of deep copy deepclone
Use Preparedstatement to select and display recorded JDBC programs
一加OnePlus 10T的一系列规格在产品发布前被披露
PPPoE协议讲解以及拨号过程Wireshark抓包解析
程序员最想干的三件事 |漫画
Single cell thesis record (part19) -- a comprehensive comparison on cell type composition information for St data
别再问我MySQL为啥没走索引?就这几种原因,全都告诉你
sns_ sensor_ instance_ api
Differences between nvisual generic cabling management software and network management software
Single cell literature learning (part6) -- forestfireclustering for SC sequencing combinations iterative label promotion with
工业物联网中的时序数据
基于C语言开发的控制台计算器
Three things programmers want to do most | comics