当前位置:网站首页>圆环回原点问题-字节跳动高频题
圆环回原点问题-字节跳动高频题
2022-04-23 17:32:00 【hequnwang10】
一、题目描述
圆环上有10个点,编号为0~9。从0点出发,每次可以逆时针和顺时针走一步,问走n步回到0点共有多少种走法。
示例 1:
输入: 2
输出: 2
解释:有2种方案。分别是0->1->0和0->9->0
二、解题
动态规划
这题类似于爬楼梯,使用动态规划:走n步到0的方案数=走n-1步到1的方案数+走n-1步到9的方案数。
dp[i][j]表示从0点出发走i步到j点的方案数。
状态转移方程:
- 走i步走到j的有n中方法 等于 {走i-1步到 j-1} + {走 i-1步到j+1},因为有回环,所以需要走到j-1可能为负数
- dp[i][j] = dp[i-1][(j-1+length)%length] + dp[i-1][(j+1+length)%length];
class Solution {
public int backToOrigin(int k) {
int[][] dp = new int[k + 1][10];
dp[0][0] = 1;
for (int i = 1; i <= k ; i++) {
for (int j = 0; j < 10; j++) {
dp[i][j] = dp[i - 1][(j - 1 + 10) % 10] + dp[i - 1][(j + 1) % 10];
}
}
return dp[k][0];
}
}
延伸
class Solution {
public int backToOrigin(int n, int k) {
if (n == 0) {
return 1;
}
if (n == 2) {
if (k % 2 == 0) {
return k;
} else {
return 0;
}
}
int[][] dp = new int[k + 1][n];
dp[0][0] = 1;
for (int i = 1; i < k + 1; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = dp[i - 1][(j - 1 + n) % n] + dp[i - 1][(j + 1) % n];
}
}
return dp[k][0];
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://blog.csdn.net/hequnwang10/article/details/124332502
边栏推荐
- SiteServer CMS5. 0 Usage Summary
- Collection of common SQL statements
- Perception of linear algebra 2
- Abnormal resolution of Xiaomi camera
- Deep understanding of control inversion and dependency injection
- 1217_使用SCons生成目标文件
- Come out after a thousand calls
- matlab如何绘制已知公式的曲线图,Excel怎么绘制函数曲线图像?
- Advantages and disadvantages of several note taking software
- How to change input into text
猜你喜欢

Deep understanding of control inversion and dependency injection

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory

Signalr can actively send data from the server to the client

For the space occupation of the software, please refer to the installation directory

2. Electron's HelloWorld

. net cross platform principle (Part I)

STM32 entry development board choose wildfire or punctual atom?

Devexpress GridView add select all columns

01-初识sketch-sketch优势

Advantages and disadvantages of several note taking software
随机推荐
Promise (IV)
双闭环直流调速系统matlab/simulink仿真
Preliminary understanding of promse
Compare the performance of query based on the number of paging data that meet the query conditions
Read software engineering at Google (15)
基于51单片机红外无线通讯仿真
flink 学习(十二)Allowed Lateness和 Side Output
Using quartz under. Net core -- a simple trigger of [7] operation and trigger
Using quartz under. Net core -- preliminary understanding of [2] operations and triggers
Use between nodejs modules
1217_使用SCons生成目标文件
Clickhouse SQL operation
Understanding and small examples of unity3d object pool
EF core in ASP Generate core priority database based on net entity model
Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
STM32 entry development board choose wildfire or punctual atom?
【WPF绑定3】 ListView基础绑定和数据模板绑定
Node template engine (EJS, art template)
Using quartz under. Net core - calendar of [6] jobs and triggers
1-2 characteristics of nodejs