爬楼梯算法

计算跳到n阶的跳法总数
package com.zxj.algorithm.动态规划;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

/**
 * 递归函数 f(n): 计算跳到n阶的跳法总数。
 * <p>
 * f(0) = 0,
 * f(1) = 1,
 * f(2) = 2,
 * f(n) = f(n-1) + f(n-2)
 */
@Slf4j
public class 爬楼梯 {

    /**
     * 备忘录
     *
     * @param data index层的跳法
     * @param n    台阶数
     * @return 返回第n层的跳法
     */
    public static int algorithm(int[] data, int n) {
        if (n == 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 2;
        }
        if (data[n] == -1) {
            data[n] = algorithm(data, n - 1) + algorithm(data, n - 2);
        }
        return data[n];
    }

    /**
     * 动态规划
     *
     * @param data index层的跳法
     * @param n    台阶数
     * @return 返回第n层的跳法
     */
    public static int algorithm2(int[] data, int n) {
        if (n == 0) {
            return 0;
        } else if (n == 1) {
            return 1;
        } else if (n == 2) {
            return 2;
        } else {
            data[1] = 1;
            data[2] = 2;
            for (int i = 3; i <= n; i++) {
                data[i] = data[i - 1] + data[i - 2];
            }
            return data[n];
        }
    }

    public static void main(String[] args) {
        long startTs;
        int n;
        int total;

        startTs = System.currentTimeMillis();
        n = 10;
        int[] data = new int[n + 1];
        Arrays.fill(data, -1);
        total = algorithm(data, n);
        log.info("备忘录 number={}, ts={}, total={}", n, System.currentTimeMillis() - startTs, total);

        startTs = System.currentTimeMillis();
        n = 1000;
        data = new int[n + 1];
        Arrays.fill(data, -1);
        total = algorithm(data, n);
        log.info("备忘录 number={}, ts={}, total={}", n, System.currentTimeMillis() - startTs, total);

        startTs = System.currentTimeMillis();
        n = 10;
        data = new int[n + 1];
        Arrays.fill(data, -1);
        total = algorithm2(data, n);
        log.info("动态规划 number={}, ts={}, total={}", n, System.currentTimeMillis() - startTs, total);

        startTs = System.currentTimeMillis();
        n = 1000;
        data = new int[n + 1];
        Arrays.fill(data, -1);
        total = algorithm2(data, n);
        log.info("动态规划 number={}, ts={}, total={}", n, System.currentTimeMillis() - startTs, total);
    }
}

运行结果

相关推荐

  1. 算法:70. 楼梯

    2023-12-13 09:04:02       27 阅读
  2. 面试算法-40-楼梯

    2023-12-13 09:04:02       39 阅读
  3. 算法学习:动态规划之楼梯问题

    2023-12-13 09:04:02       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-13 09:04:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-13 09:04:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-13 09:04:02       87 阅读
  4. Python语言-面向对象

    2023-12-13 09:04:02       96 阅读

热门阅读

  1. Jenkins 设置中文

    2023-12-13 09:04:02       58 阅读
  2. 《C++新经典设计模式》之第7章 单例模式

    2023-12-13 09:04:02       46 阅读
  3. Go 语言开发工具

    2023-12-13 09:04:02       61 阅读
  4. 云计算在数据处理中的应用

    2023-12-13 09:04:02       65 阅读
  5. Tomcat

    2023-12-13 09:04:02       50 阅读
  6. css表单

    css表单

    2023-12-13 09:04:02      58 阅读
  7. Linux下的网络服务

    2023-12-13 09:04:02       54 阅读
  8. 为 PHP 引入 Python 生态的经验分享

    2023-12-13 09:04:02       65 阅读
  9. 【MODBUS】libmodbus库写一个Modbus TCP客户端

    2023-12-13 09:04:02       60 阅读