当前位置:网站首页>Algorithm --- different paths (kotlin)
Algorithm --- different paths (kotlin)
2022-07-28 13:52:00 【Xiaomi technology Android research and development caoxinyu】
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 :
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
source : Power button (LeetCode)
link :https://leetcode.cn/problems/unique-paths
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Solutions

resolvent
fun uniquePaths(m: Int, n: Int): Int {
val dp = Array(m) {
Array(n) {
0 } }
dp[0][0] = 1
for (i in 0 until m) {
for (j in 0 until n) {
if (i == 0 && j ==0) continue
val left = if (i - 1 >= 0) dp[i - 1][j] else 0
val top = if (j - 1 >= 0) dp[i][j - 1] else 0
dp[i][j] = left + top
println(" $i $j ${
dp[i][j]}")
}
}
return dp[m - 1][n - 1]
}
summary
1. The simplest in others' eyes , Up to now, I have no idea . After reading the solution to the question, I know what the idea is .
边栏推荐
- 30天刷题训练(一)
- 酷炫操作预热!代码实现小星球特效
- Holes in [apue] files
- R language ggplot2 visualization: visualize the scatter diagram and add text labels to the data points in the scatter diagram, using geom of ggrep package_ text_ The rep function avoids overlapping da
- Generation of tables and contingency tables (cross tables) of R language factor data: use the summary function to analyze the list, view the chi square test results, and judge whether the two factor v
- [Architecture] reading notes of three micro service books with high scores
- R language test sample proportion: use prop The test function performs the single sample proportion test to calculate the confidence interval of the p value of the successful sample proportion in the
- Use non recursive method to realize layer traversal, preorder traversal, middle order traversal and post order traversal in binary tree
- 不用Swagger,那我用啥?
- Is azvudine, a domestic oral new coronal drug, safe? Expert authority interpretation
猜你喜欢

111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误

7. Dependency injection

在 Kubernetes 中部署应用交付服务(第 1 部分)
![[security] read rfc6749 and understand the authorization code mode under oauth2.0](/img/dc/e6d8626195b2e09a6c06050a9b552e.jpg)
[security] read rfc6749 and understand the authorization code mode under oauth2.0

Three men "running away" from high positions in the mobile phone factory

对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解

Beyond istio OSS -- current situation and future of istio Service Grid

30 day question brushing plan (II)

After finishing, help autumn move, I wish you call it an offer harvester

30 day question brushing plan (III)
随机推荐
Poj3268 shortest path solution
微信小程序中自定义模板
R语言使用lm函数构建多元回归模型(Multiple Linear Regression)、并根据模型系数写出回归方程、使用confint函数给出回归系数的95%置信区间
C language: merge sort
Using fail2ban to protect web servers from DDoS Attacks
After finishing, help autumn move, I wish you call it an offer harvester
PHP generates random numbers (nickname random generator)
C language: random number + quick sort
比XShell更好用、更现代的终端工具!
P1797 heavy transportation problem solution
leetcode(442)数组中重复的数据
30天刷题计划(二)
Go language - Application of stack - expression evaluation
Countdown 2 days! 2022 China Computing Conference: Mobile cloud invites you to meet with computing network for innovative development
了解虚拟列表背后原理,轻松实现虚拟列表
No swagger, what do I use?
leetcode-深度优先与广度优先遍历
I'm bald! Who should I choose for unique index or general index?
How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question
Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022