当前位置:网站首页>Leetcode DP three step problem
Leetcode DP three step problem
2022-07-02 23:33:00 【qq5924db5b70f63】
package dp.waysToStep;
/**
* Interview questions 08.01. Three step problem
* Three step problem . There is a child going up the stairs , The stairs have n Steps , Children can go to 1 rank 、2 Step or 3 rank . Implement a way , Calculate how many ways a child can go up stairs . The results could be big , You need to model the results 1000000007.
* <p>
* Example 1:
* <p>
* Input :n = 3
* Output :4
* explain : There are four ways to walk
* Example 2:
* <p>
* Input :n = 5
* Output :13
* Tips :
* <p>
* n The scope is [1, 1000000] Between
*/
public class waysToStep {
public static int waysToStep(int n) {
int size = n<3?3:n;
int[] dp = new int[size];
dp[0] = 1;
dp[1] = 2;
dp[2] = 4;
for (int i = 3; i < n; i++) {
//dp[i-3] The mold has been taken in the last step , The remaining two to prevent overflow also need to take the mold
dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007 + dp[i - 3];
dp[i] %= 1000000007;
}
return dp[n-1];
}
public static void main(String[] args) {
System.out.println(waysToStep(1));
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
Can't , I can learn ; backward , I can catch up ; Fall down , I can stand up !
边栏推荐
猜你喜欢
Markdown basic grammar
Potplayer set minimized shortcut keys
Convolution和Batch normalization的融合
Interface switching based on pyqt5 toolbar button -1
Is 408 not fragrant? The number of universities taking the 408 examination this year has basically not increased!
采用VNC Viewer方式遠程連接樹莓派
Redis expiration policy +conf record
Win11启用粘滞键关闭不了怎么办?粘滞键取消了但不管用怎么解决
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
Three solutions to frequent sticking and no response of explorer in win11 system
随机推荐
YOLOX加强特征提取网络Panet分析
Which common ports should the server open
MySQL基础
Intranet penetration | teach you how to conduct intranet penetration hand in hand
【ML】李宏毅三:梯度下降&分类(高斯分布)
Submit code process
Where is the win11 microphone test? Win11 method of testing microphone
Use redis to realize self increment serial number
简述中台的常识
MySQL Foundation
请求与响应
Speech recognition Series 1: speech recognition overview
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
Quantitative analysis of PSNR, SSIM and RMSE
聊聊内存模型与内存序
Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
采用VNC Viewer方式遠程連接樹莓派
【直播预约】数据库OBCP认证全面升级公开课
Solving ordinary differential equations with MATLAB
Writing of head and bottom components of non routing components