当前位置:网站首页>剑指Offer 71:跳台阶扩展问题
剑指Offer 71:跳台阶扩展问题
2022-07-19 19:58:00 【斯沃福德】
链接
题目:
思路:动态规划
逆向思维,在n阶台阶上往下走,共有1+2+…n种方法;
所以f(n) = f(n−1)+f(n−2)+…+f(n−(n−1))+f(n−n) => f(0)+f(1)+f(2)+…+f(n−1)
因为f(n−1)=f(n−2)+f(n−3)+…+f((n−1)−(n−2))+f((n−1)−(n−1));
经整理得f(n) = f(n−1)+f(n−1) = 2∗f(n−1) ,因此每级台阶方案数是前面一级台阶方案数的2倍。
import java.util.*;
public class Solution {
int[] memo;
public int jumpFloorII(int n) {
memo=new int[n+1];
Arrays.fill(memo,-1);
return dp(n);
}
//
int dp(int n){
//base case
if(n<=2){
return n;
}
//重叠子问题
if(memo[n]!=-1){
return memo[n];
}
memo[n]=dp(n-1) * 2;
return memo[n];
}
}
边栏推荐
- Animation, and basic use of animation
- What is the difference between server bandwidth and home broadband?
- NFT access tool premint was hacked, with a loss of more than 370000 US dollars
- Overview | comprehensive comparative research on image denoising
- 短視頻系統源碼,uni-app項目中主要文件的加載順序
- Alternative interpretation of the macro situation: the Federal Reserve may soon end the process of raising interest rates and return to quantitative easing?
- Source code of short video system, loading order of main files in uni app project
- go 操作 Excel
- 面试必考:秒杀系统要如何设计?
- MySQL basic learning day06
猜你喜欢
[today in history] July 19: the father of IMAP agreement was born; Project kotlin made a public appearance; New breakthroughs in CT imaging
山西省第二届网络安全技能大赛(企业组)部分赛题WP(五)
Skynet天网监测到的数笔可疑交易背后:又一欺诈项目Forest Tiger Pro被确认
What is the difference between server bandwidth and home broadband?
go 操作 Excel
牛客每日刷题之数组
[untitled]
Overview | comprehensive comparative research on image denoising
Alternative interpretation of the macro situation: the Federal Reserve may soon end the process of raising interest rates and return to quantitative easing?
Leetcode interview question 05.06 Integer conversion
随机推荐
DNS原理及配置
数据库设计流程
[development tutorial 5] crazy shell arm function mobile phone serial port experiment tutorial
迭代器(iterator)的简介及使用案例
生成器的使用原则及方法以及利用生成器实现简易项目(内含详细解说传参问题)
67亿美元!瑞萨电子收购IDT获得最终审批通过!
leetcode 面试题 05.06. 整数转换
HTC全新VR一体机Vive Focus Plus发布:定价5699元!
[combinational logic circuit] - display decoder
索尼宣布关闭北京手机工厂!产线将迁至泰国,成本可降低一半!
如何将 NFT 元数据从 IPFS 转移到智能合约中
Unity3D学习笔记9——加载纹理
What are the four differences between intranet and extranet?
远程登陆----radius认证
How to implement recursive function in wechat game making tool
面试官一口气问了MySQL事务、锁和MVCC,我
finance × Yuancosmos: financial system under the integration of reality and emptiness
Easy gene chip SEQ analysis method: practical workflow and advanced applications
突发!英国指控华为设备存在重大缺陷(附报告)
Firewall related