小红书1+0.09+0.18
枚举了很多情况 但没分
枚举了很多情况 但没分
全部评论
0.54 + 0.36 + 1
,第一题不知道为啥答案错误,第二题深搜超时了,记忆化没写出来
我是1+0.18+0.09
第二题记忆化搜索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);
}
}
最后一道竟然给出个运行错误提示,找了半天也没找到哪有问题😨
相关推荐
点赞 评论 收藏
分享