商汤科技的笔试题。n阶,步长1~m,有时间和内存限制。

基础题:

n阶楼梯,每次能走1或2阶,问走到n阶一共多少种走法。

法一:递归 很容易超时

f(0)=1,f(1)=1,f(n)=f(n-1)+f(n-2)

#include <stdio.h>
#include <iostream>
#include<Windows.h>
using namespace std;long long climbStairs(int n)
{if (n == 1)return 1;else if (n == 2)return 2;else if (n > 2)return climbStairs(n - 1) + climbStairs(n - 2);
}int main()
{long long stepCount = 0;DWORD start_time = GetTickCount();stepCount = climbStairs(45);  //40: 165580141 4.88s 45: 1836311903 53.99sDWORD end_time = GetTickCount();printf("%lld\n", stepCount);cout << "The run time is: " << (end_time - start_time) / 1000.0 << " s!" << endl;getchar();return 0;
}

法二:递推 用空间换时间,避免了重复计算,时间快了很多倍

#include <stdio.h>
#include <iostream>
#include<Windows.h>
using namespace std;long long step[101] = { 0 };
long long climbStairs(long long step[], int n)
{if (n <= 0)return 0;step[0] = 1;//人为规定step[1] = 1;if (n >= 2){for (long long i = 2; i <= n; i++){step[i] = step[i - 1] + step[i - 2];}}return step[n];
}int main()
{long long stepCount = 0;DWORD start_time = GetTickCount();stepCount = climbStairs(step, 40);DWORD end_time = GetTickCount();printf("%lld\n", stepCount);cout << "The run time is: " << (end_time - start_time) / 1000.0 << " s!" << endl;getchar();return 0;
}

法三:找数学规律 类似于递推 求得通项 [Xn Xn-1]=[Xn-1+Xn-2;Xn-1]=...=[1 1;1 0]^(n-1) * [1 1]

#include <stdio.h>
#include <iostream>
#include<Windows.h>
using namespace std;long long climbStairs(int n)
{int a11 = 1, a12 = 1, a21 = 1, a22 = 0;int v1 = 1, v2 = 1;int x1 = 1, x2 = 1;for (int i = 1; i < n; i++){x1 = a11*v1 + a12*v2;x2 = a12*v1 + a22*v2;v1 = x1;v2 = x2;}return v1;
}int main()
{long long stepCount = 0;DWORD start_time = GetTickCount();stepCount = climbStairs(40);DWORD end_time = GetTickCount();printf("%lld\n", stepCount);cout << "The run time is: " << (end_time - start_time) / 1000.0 << " s!" << endl;getchar();return 0;
}

拓展:N阶台阶,每次可以走1~m步

法一:递归 时间容易超

#include <stdio.h>
#include <iostream>
#include<Windows.h>
using namespace std;long long climbStairs(int n, int m) {int stepsCount = 0;if (n == 0) {return 1;}if (n >= m) {// 总剩余的台阶数n大于步长mfor (int i = 1; i <= m; i++) {stepsCount += climbStairs(n - i, m);}}else {// 剩余台阶数n小于步长mstepsCount += climbStairs(n, n);}return stepsCount;
}int main()
{long long stepCount = 0;DWORD start_time = GetTickCount();stepCount = climbStairs(45,2);//40:165580141 14.898s 45:1836311903 166sDWORD end_time = GetTickCount();printf("%lld\n", stepCount);cout << "The run time is: " << (end_time - start_time) / 1000.0 << " s!" << endl;getchar();return 0;
}

上面这个代码太初级,只能解决n和m都很小的情况,通过了50%。题目中实际要求时间<=2s。

需要优化。待更新。

Reference:https://blog.crayygy.com/14599905787744.html

爬楼梯问题/上台阶问题相关推荐

  1. 程序设计爬楼梯问题_楼梯案例:解决楼梯问题的C ++程序

    程序设计爬楼梯问题 A child is running up a staircase with N steps, and can hop 1 step, 2 steps or 3 steps at ...

  2. LeetCode两个爬楼梯题目解析(动态规划)

    原题: https://leetcode-cn.com/problems/climbing-stairs/description/ https://leetcode-cn.com/problems/m ...

  3. Java算法——爬楼梯(LeetCode第70题)

    问题描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 示例 分析 题目中给出,上台阶的方式只有两种,一种为一步跨一阶,一种为 ...

  4. LeetCode简单题之爬楼梯

    题目 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 示例 1: 输入:n = 2 输出:2 解释:有两种方法可以爬到楼顶. 1 ...

  5. LeetCode实战:爬楼梯

    题目英文 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...

  6. 【每日一算法】爬楼梯

    微信改版,加星标不迷路! 每日一算法-爬楼梯 作者:阿广 阅读目录 ? 题目 ? 解析 ? 完整代码 1 题目 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多 ...

  7. msteel能计算钢结构楼梯吗_坚持爬楼梯能减肥吗 怎么爬楼梯可以减肥

    现在很多高楼都装有电梯,因此很多人都偏向于挤电梯而不是爬楼梯.即使自己所在的楼层很低都是这样的.但其实适当的爬楼梯是有利于锻炼身体的,据说还有减肥的功效.那么你觉得坚持爬楼梯能减肥吗?下面我们一起去健 ...

  8. 计蒜客 挑战难题 爬楼梯

    计蒜客 挑战难题 爬楼梯 假设你现在正在爬楼梯,楼梯有n级.每次你只能爬1级或者2级,那么你有多少种方法爬到楼梯的顶部? 格式: 第一行输入一个数n(n<=50),代表楼梯的级数. 接下来一行输 ...

  9. 《LeetCode力扣练习》第70题 爬楼梯 Java

    <LeetCode力扣练习>第70题 爬楼梯 Java 一.资源 题目: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶 ...

最新文章

  1. 【官网搭建】在网站首页底部添加备案号链接至工信部首页及版权所有。
  2. 产品经理成长三五事儿:搭建自己的成长模型
  3. 安装 paddleocr 报错 gcc: error trying to exec ‘cc1‘: execvp: 没有那个文件或目录
  4. 知道ThreadLocal吗?一起聊聊到底有啥用
  5. Django中的form模块的高级处理
  6. 移除Win10资源管理器中OneDrive图标
  7. saltstack学习笔记
  8. linux shell脚本读取配置文件 val=1,shell脚本
  9. Raki的读paper小记:Audio Captioning with Composition of Acoustic and Semantic Information
  10. 推荐一个下载中国城市统计年鉴的地址
  11. 使用最小二乘法计算多元线性回归的公式推导
  12. 【联邦学习】隐私计算理论和效率
  13. 惯性系统常用坐标系_惯性技术常用坐标系
  14. 电脑怎么压缩jpg图片?如何压缩照片并保持清晰?
  15. 外币记账及重估总账余额表变化(下)
  16. phrases practice_Choose any passage from unit 3 and unit 4 to practice.
  17. xshell中文免费下载及安装 (内附xshell下载链接以及安装步骤)
  18. 搜索引擎(大数据检索)论述[elasticsearch原理相关]
  19. 使用python进行视频截取
  20. 学生宿舍管理项目开发计划书_学生宿舍管理系统项目计划书.doc

热门文章

  1. 由 method may be static 引发的 Python PEP8 总结
  2. 计算机教育叙事,计算机在小学语文教学中运用的教育叙事
  3. 面包板入门--点亮LED
  4. 值得推荐的C/C++框架和库 (真的很强大)
  5. python调用excel的宏_在 Excel 中使用 Python 开发宏脚本
  6. YUY2(YUYV)转YUV420源码分析
  7. vivo Y66解账户锁刷机包 线刷包救砖教程
  8. [深入理解SSD 为SSD编程] 访问模式和系统优化
  9. JavaScript 中清空数组的几种方法
  10. CentOS7使用yum安装MySQL5版本