当前位置:网站首页>LeetCode#62. Different paths
LeetCode#62. Different paths
2022-07-06 15:21:00 【Rufeng ZHHH】
subject :
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 = 3, n = 2
Output :3
explain :
From the top left corner , All in all 3 Path to the bottom right .
1. towards the right -> Down -> Down
2. Down -> Down -> towards the right
3. Down -> towards the right -> Down
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 * 109
source : Power button (LeetCode)
link :62. Different paths - Power button (LeetCode) (leetcode-cn.com)
Method 1 ( Permutation and combination ):
When I first saw this problem, I first thought of it in Mathematics Permutation and combination ( A kind of Combinatorial Mathematics )_ Baidu Encyclopedia (baidu.com), Many high school students must have done this kind of problem , The method is also relatively simple .
Let's assume that we have to go altogether m+n Step , Then just let one of them m Step right , remainder n Step must be down , The calculation is C(m+n,n)=C(m+n,m).
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
son=min(m-1,n-1);mother=m+n-2
up=1;down=1
for i in range(son):
up*=son-i
for i in range(son):
down*=mother-i
if int(down/up)==0:
return 1
else:
return int(down/up)Method 2 ( Dynamic programming ):
Let's suppose we get to the k Step , Then the general method of the previous steps is the first two k-1 Sum of steps ( From the top left to the bottom right can only be the last step, coming from the left or the top , Otherwise it will be meaningless ), It's just like this in terms of images .

So we need to create a two-dimensional list :
dp=[]
for i in range(m):
dp.append([1]*n) # The default starting values are 1After creation , It can be calculated according to this idea , This is similar to Yang Hui's Algorithm LeetCode#118. Yang hui triangle _ As the wind Zhhh The blog of -CSDN Blog
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
dp=[]
for i in range(m):
dp.append([1]*n)
for i in range(m-1):
for j in range(n-1):
dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j]
return dp[m-1][n-1]边栏推荐
- Mysql database (III) advanced data query statement
- 软件测试行业的未来趋势及规划
- The most detailed postman interface test tutorial in the whole network. An article meets your needs
- MySQL数据库(五)视 图 、 存 储 过 程 和 触 发 器
- FSM和i2c实验报告
- Mysql database (II) DML data operation statements and basic DQL statements
- Cadence physical library lef file syntax learning [continuous update]
- Take you to use wxpy to create your own chat robot (plus wechat interface basic data visualization)
- ucorelab4
- UCORE lab5 user process management experiment report
猜你喜欢

Stc-b learning board buzzer plays music 2.0

Brief introduction to libevent
What to do when programmers don't modify bugs? I teach you
Automated testing problems you must understand, boutique summary

Take you to use wxpy to create your own chat robot (plus wechat interface basic data visualization)

Your wechat nickname may be betraying you
遇到程序员不修改bug时怎么办?我教你

How to write the bug report of software test?

ucore lab 6

51 lines of code, self-made TX to MySQL software!
随机推荐
[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)
Global and Chinese markets for complex programmable logic devices 2022-2028: Research Report on technology, participants, trends, market size and share
C4D quick start tutorial - creating models
Leetcode notes - dynamic planning -day7
软件测试行业的未来趋势及规划
Crawler series of learning while tapping (3): URL de duplication strategy and Implementation
Knowledge that you need to know when changing to software testing
ArrayList集合
The minimum number of operations to convert strings in leetcode simple problem
MySQL数据库(二)DML数据操作语句和基本的DQL语句
MySQL transactions
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
ucore lab1 系统软件启动过程 实验报告
Example 071 simulates a vending machine, designs a program of the vending machine, runs the program, prompts the user, enters the options to be selected, and prompts the selected content after the use
STC-B学习板蜂鸣器播放音乐2.0
UCORE lab5 user process management experiment report
ucore Lab 1 系统软件启动过程
The latest query tracks the express logistics and analyzes the method of delivery timeliness
STC-B学习板蜂鸣器播放音乐
基于485总线的评分系统双机实验报告