当前位置:网站首页>Sword finger offer 10- ii Frog jumping on steps (4 solutions)
Sword finger offer 10- ii Frog jumping on steps (4 solutions)
2022-07-18 22:07:00 【[email protected]】

tag: Simple dynamic programming
Write it down as dp[n] Express n The jumping of steps , Arrive at n Steps can be taken from the n-2 Step up to , You can also start with n-1 Step up to , Therefore, the number of n Step scheme tree = Arrive at n-1 Number of schemes for steps + Arrive at n-2 Number of schemes for steps , namely dp[n]=dp[n-2]+dp[n-1]
solution 1:
Use dp Array
class Solution {
int mod=(int)(1e9+7);
public int numWays(int n) {
if(n<=1){
return 1;
}
int[] dp=new int[n+1];
dp[0]=1;
dp[1]=1;
for(int i=2;i<=n;i++){
dp[i]=dp[i-2]+dp[i-1];
dp[i]%=mod;
}
return dp[n];
}
}
//O(n)
//O(n)
solution 2:
Use variables instead dp Array
class Solution {
int mod=(int)(1e9+7);
public int numWays(int n) {
if(n<=1){
return 1;
}
int dp0=1;
int dp1=1;
int dp2=-1;
for(int i=2;i<=n;i++){
dp2=dp0+dp1;
dp2%=mod;
dp0=dp1;
dp1=dp2;
}
return dp2;
}
}
//O(n)
//O(1)
solution 3:
recursive + Memory search
class Solution {
int mod=(int)(1e9+7);
int[] memo;
public int numWays(int n) {
if(memo==null){
memo=new int[n+1];
}
if(n<=1){
return 1;
}
if(memo[n]!=0){
return memo[n];
}else{
memo[n]=numWays(n-2)+numWays(n-1);
memo[n]%=mod;
return memo[n];
}
}
}
//O(n)
//O(n)
solution 4:
Fast power , Reference resources The finger of the sword Offer 10- I. Fibonacci sequence (4 A solution ) No 4 A solution , Just modify the initial dp[0] value , In the Fibonacci series dp[0]=0, here dp[0]=1
class Solution {
int mod=(int)(1e9+7);
public int numWays(int n) {
if(n<=1){
return 1;
}
long[][] mat={
{
1,1},
{
1,0}
};
long[][] ans={
{
1},
{
1}
};
int x=n-1;
// Fast power algorithm
while(x!=0){
if(x%2==1){
ans=multi(mat,ans);
}
mat=multi(mat,mat);
x/=2;
}
return (int)ans[0][0];
}
// Matrix multiplication algorithm
public long[][] multi(long[][] a,long[][] b){
//a:p*q b: q*r
int p=a.length,q=a[0].length,r=b[0].length;
long[][] ans=new long[p][r];
for(int i=0;i<p;i++){
for(int j=0;j<r;j++){
for(int k=0;k<q;k++){
ans[i][j]+=a[i][k]*b[k][j];
ans[i][j]%=mod;
}
}
}
return ans;
}
}
//O(logn)
//O(1)
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/199/202207161919163552.html
边栏推荐
- 栓Q了,大厂被强制毕业,空窗一个月死背八股文,还好拿到了Offer
- 通感一体化融合架构及关键技术
- Istio gray Publishing: deploy bookinfo microservice project
- The upgraded ranking activity is hot again. Looking around, it's full of bonuses
- Calculate text similarity based on gensim
- 网络基础VlAN配置 Trunk技术(eNSP、Cisco)
- How to use mitmproxy to get data return in automated testing?
- Inventory of major RPA products at home and abroad
- C语言-数组
- How to make underline style of selected items
猜你喜欢

Installing MySQL database on Linux server (detailed tutorial)

Leetcode 1342. 将数字变成 0 的操作次数

Linux 解决 Oracle :ORA-12537: TNS:connection closed(连接关闭)问题

RPA生态系统大揭秘,支撑RPA企业数十亿估值的生命本源

Istio灰度發布:部署Bookinfo微服務項目

Win11怎么打开3D查看器

Application of Apache abox-700 industrial computer minipicecan card in electric power inspection robot

surging作者出具压测结果

RPA ecosystem revealed, supporting the life source of RPA enterprises' billions of valuation

降噪蓝牙耳机哪款好?性价比最高的主动降噪蓝牙耳机
随机推荐
传输层 三次握手中性能优化
鎳氫電池的特性和使用方法(FDK鎳氫電池充電機制)
Istio gray Publishing: deploy bookinfo microservice project
MySQL variables, process control and cursor exercises
低代码开发搭建业务流程管理解决方案
Spark Streaming 编程指南
Installing MySQL database on Linux server (detailed tutorial)
机器学习实战运用:速刷牛客5道机器学习题目
Linux 解决 Oracle :ORA-12537: TNS:connection closed(连接关闭)问题
Leetcode 1309. 解码字母到整数映射(可以,一次过)
Comparison and reference of collaborative office system (OA system) selection in 2022
Key technologies and challenges of communication perception integration
The scroll up and down switch of the flutter text is used to prompt the announcement message
Huawei and glory mobile phones cannot cooperate with matebook on multiple screens after upgrading the Hongmeng system
深入剖析多重背包问题(下篇)
我的创作纪念日
阿普奇 E7系列 工控机——MinipiceCAN卡在送餐机器人中的应用
(note sorting is not completed) [graph theory: minimum spanning tree]
【图文并茂】U盘启动盘制作 U盘启动盘重装系统教程
regular expression