Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
Home
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 设计模式
  • JavaSE
  • JVM
  • JUC
  • Netty
  • CPP
  • QT
  • UE
  • Go
  • Gin
  • Gorm
  • HTML
  • CSS
  • JavaScript
  • vue2
  • TypeScript
  • vue3
  • react
  • Spring
  • SpringMVC
  • Mybatis
  • SpringBoot
  • SpringSecurity
  • SpringCloud
  • Mysql
  • Redis
  • 消息中间件
  • RPC
  • 分布式锁
  • 分布式事务
  • 个人博客
  • 弹幕视频平台
  • API网关
  • 售票系统
  • 消息推送平台
  • SaaS短链接系统
  • Linux
  • Docker
  • Git
GitHub (opens new window)
  • 哈希表

  • 双指针

  • 数组

  • 字符串

  • 链表

  • 树

  • 回溯

  • 动态规划

  • 图

  • 二分查找

  • 贪心

  • 栈&队列

  • 堆

  • 位运算

  • 数据结构设计

  • rui的精选题单

  • 笔试真题

    • 数组
    • 二分
    • 树
    • 动态规划
      • 线性dp
        • 跳格子[华为]
        • 士兵高度[联通]
    • 回溯
    • 图
  • LeetCode周赛

  • ACM模式输入输出
  • 数学

  • 数据结构与算法
  • 笔试真题
Nreal
2024-01-08
目录

动态规划

# 线性dp

# 跳格子[华为]

题目描述:

每个格子有魔法值,代表可以跳得最远距离;最多可以跳 k 次,求跳到终点的最小步数;

案例:

输入:

2 1 5 6 2 3

3

输出:

2

方法一:记忆化搜索

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        powers = Arrays.stream(sc.nextLine().split(" "))
                .mapToInt(Integer::valueOf)
                .toArray();
        int n = powers.length;
        int k = sc.nextInt();
        memo = new int[n];
        Arrays.fill(memo,-1);
        int ans = dfs(0);
        if(ans<=k){
            System.out.println(ans);
        }else{
            System.out.println(-1);
        }
    }
    static int[] memo;
    static int[] powers;
    static int dfs(int i){
        // 目前位置到达终点
        if(i>=powers.length-1){
            return 0;
        }
        if(memo[i]!=-1){
            return memo[i];
        }
        int ans = Integer.MAX_VALUE;
        // 枚举在 i 位置可跳的步数
        for(int j=1;j<=powers[i];j++){
            ans = Math.min(ans,1+dfs(i+j));
        }
        return memo[i] = ans;
    }
}
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

方法二:动态规划

见45. 跳跃游戏 II (opens new window)

class Solution {
    public int jump(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp,Integer.MAX_VALUE/2);
        dp[0] = 0;
        for(int i=1;i<n;i++){
            for(int j=0;j<i;j++){
                if(i-j<=nums[j]){
                    dp[i] = Math.min(dp[j]+1,dp[i]);
                }
            }
        }
        return dp[n-1];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 士兵高度[联通]

**输入样例:**3 4 1 2 3 4 3 1 1 1 4 1 1 3 2 **输出样例:**4 1 2

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      int total = Integer.valueOf(reader.readLine());
      for(int k=0;k<total;k++){
        int n = Integer.valueOf(reader.readLine());
        int[] nums = Arrays.stream(reader.readLine().split(" "))
          .mapToInt(Integer::valueOf)
          .toArray();
        int[] dp = new int[n];
        Arrays.fill(dp,1);
        int ret = 0;
        for(int i=0;i<n;i++){
          for(int j=0;j<i;j++){
            if(nums[i]>nums[j]){
              dp[i] = Math.max(dp[j]+1,dp[i]);
            }
          }
          ret = Math.max(ret,dp[i]);
        }
        System.out.println(ret);
      }
    }
}

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
树
回溯

← 树 回溯→

Theme by Vdoing | Copyright © 2021-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式