关注
第二题记忆化搜索package 秋招笔试.小红书0806.第2题;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
long[][] nums = new long[n][3];
int totalTime = scanner.nextInt(), totalEnergy = scanner.nextInt();
scanner.nextLine();
for (int i = 0; i < n; ++i) {
nums[i] = new long[]{scanner.nextLong(), scanner.nextLong(), scanner.nextLong()};
scanner.nextLine();
}
long[][][] dp = new long[501][501][nums.length];
for (long[][] d : dp)
for (long[] arr : d)
Arrays.fill(arr, -1);
long res = dfs(nums, totalTime, totalEnergy, 0, dp);
System.out.println(res);
}
private static long dfs(long[][] nums, int totalTime, int totalEnergy, int index, long[][][] dp) {
if (index == nums.length)
return 0;
if (dp[totalTime][totalEnergy][index] != -1)
return dp[totalTime][totalEnergy][index];
long pass = dfs(nums, totalTime, totalEnergy, index + 1, dp);
long choose = 0;
if (totalTime >= nums[index][0] && totalEnergy >= nums[index][1])
choose = dfs(nums, (int) (totalTime - nums[index][0]), (int) (totalEnergy - nums[index][1]), index + 1, dp) + nums[index][2];
return dp[totalTime][totalEnergy][index] = Math.max(pass, choose);
}
}
查看原帖
点赞 评论
相关推荐
查看12道真题和解析 点赞 评论 收藏
分享
01-30 16:13
浙江大学 Java 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 牛客新年AI问运 #
3274次浏览 79人参与
# 刚工作,应该先搞钱or搞成长? #
20855次浏览 165人参与
# 牛客AI体验站 #
15638次浏览 278人参与
# 你觉得第一学历对求职有影响吗? #
230006次浏览 1267人参与
# 如何提高实习转正率? #
85706次浏览 504人参与
# 找工作中的小确幸 #
80407次浏览 448人参与
# 实习在多还是在精 #
82796次浏览 509人参与
# 你觉得技术面多长时间合理? #
168120次浏览 1170人参与
# 牛友的春节生活 #
11965次浏览 229人参与
# 月薪多少能在一线城市生存 #
136431次浏览 898人参与
# 哪些公司对双非友好 #
206952次浏览 1163人参与
# 选了这个offer,你有没有后悔? #
738575次浏览 4472人参与
# 秋招踩过的“雷”,希望你别再踩 #
185705次浏览 1684人参与
# 备战春招/暑实,现在应该做什么? #
7943次浏览 204人参与
# 从夯到拉,锐评职场mentor #
7796次浏览 113人参与
# 实习到现在,你最困惑的一个问题 #
6988次浏览 169人参与
# 电网笔面经互助 #
59694次浏览 476人参与
# 找工作中的意难平 #
983464次浏览 6424人参与
# 制造业的秋招小结 #
143371次浏览 2089人参与
# 春招什么时候投? #
13270次浏览 211人参与
