当前位置:网站首页>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 !
边栏推荐
- Pandora IOT development board learning (HAL Library) - Experiment 3 key input experiment (learning notes)
- What if win11 can't turn off the sticky key? The sticky key is cancelled but it doesn't work. How to solve it
- Where is the win11 automatic shutdown setting? Two methods of setting automatic shutdown in win11
- Go basic constant definition and use
- JDBC教程
- 简述中台的常识
- Solving ordinary differential equations with MATLAB
- Li Kou brush questions (2022-6-28)
- 【Proteus仿真】51单片机+LCD12864推箱子游戏
- 4 special cases! Schools in area a adopt the re examination score line in area B!
猜你喜欢
Alibaba cloud award winning experience: how to use polardb-x
Tiktok actual combat ~ number of likes pop-up box
BBR encounters cubic
[ml] Li Hongyi III: gradient descent & Classification (Gaussian distribution)
采用VNC Viewer方式遠程連接樹莓派
Win11如何开启目视控制?Win11开启目视控制的方法
Mapper代理开发
[Verilog tutorial]
JDBC練習案例
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
随机推荐
Golang common settings - modify background
非路由组件之头部组件和底部组件书写
JDBC教程
cocospods 的使用
20220527_ Database process_ Statement retention
Li Kou brush questions (2022-6-28)
Submit code process
PHP get real IP
Win11如何开启目视控制?Win11开启目视控制的方法
Cryptography -- the mode of block cipher
数字图像处理实验目录
Highly available cluster (HAC)
CDN acceleration requires the domain name to be filed first
Tiktok actual combat ~ number of likes pop-up box
高数有多难?AI 卷到数学圈,高数考试正确率 81%!
Markdown basic grammar
Third party payment function test point [Hangzhou multi tester _ Wang Sir] [Hangzhou multi tester]
How difficult is it to be high? AI rolls into the mathematics circle, and the accuracy rate of advanced mathematics examination is 81%!
The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
购买完域名之后能干什么事儿?