关注
第二题记忆化搜索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);
}
}
查看原帖
点赞 评论
相关推荐
![](https://static.nowcoder.com/fe/file/oss/icon_job.png)
点赞 评论 收藏
分享
点赞 评论 收藏
分享
牛客热帖
正在热议
# 牛客帮帮团来啦!有问必答 #
1340637次浏览 18787人参与
# 非技术岗薪资爆料 #
54367次浏览 733人参与
# 极具前瞻性,现代汽车编程题 #
10374次浏览 199人参与
# 和牛牛一起刷题打卡 #
46839次浏览 3668人参与
# 产品每日一题 #
2450次浏览 116人参与
# 互联网公司评价 #
107002次浏览 1383人参与
# OPPO开奖 #
60103次浏览 860人参与
# 来聊聊你目前的求职进展 #
231217次浏览 2927人参与
# 春招你拿到offer了吗 #
420642次浏览 5957人参与
# 24届软开秋招面试经验大赏 #
1244089次浏览 18708人参与
# 硬件兄弟们 甩出你的华为奖状 #
38390次浏览 226人参与
# 晒一晒我的offer #
4045279次浏览 60495人参与
# 写简历别走弯路 #
361691次浏览 4560人参与
# 华子oc时间线 #
11857次浏览 61人参与
# 软件开发2024笔面经 #
1574734次浏览 36120人参与
# 投递实习岗位前的准备 #
756320次浏览 13182人参与
# 如果可以选,你最想从事什么工作 #
221708次浏览 3417人参与
# 运营人的第一份offer应该如何选 #
41891次浏览 694人参与
# 机械制造笔面经 #
14944次浏览 393人参与
# 我想象的工作vs实际工作 #
118026次浏览 1821人参与
# 参加过提前批的机械人,你们还参加秋招么 #
17067次浏览 385人参与
# 百度工作体验 #
32485次浏览 319人参与