华为OD机试真题 - 跳格子3
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int steps = Integer.parseInt(scanner.nextLine());
int[] nums = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int k = Integer.parseInt(scanner.nextLine());
calculateMaxScoreSum(steps, nums, k);
}
public static int calculateMaxScoreSum(int steps, int[] nums, int k) {
int[] dp = new int[steps];
dp[0] = nums[0];
for (int i = 1; i < dp.length; i++) {
int max = Integer.MIN_VALUE;
for (int j = 1; j <= k; j++) {
if (i - j >= 0)
max = Math.max(max, nums[i - j]);
}
dp[i] = nums[i] + max;
}
return dp[steps - 1];
}
Scanner scanner = new Scanner(System.in);
int steps = Integer.parseInt(scanner.nextLine());
int[] nums = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
int k = Integer.parseInt(scanner.nextLine());
calculateMaxScoreSum(steps, nums, k);
}
public static int calculateMaxScoreSum(int steps, int[] nums, int k) {
int[] dp = new int[steps];
dp[0] = nums[0];
for (int i = 1; i < dp.length; i++) {
int max = Integer.MIN_VALUE;
for (int j = 1; j <= k; j++) {
if (i - j >= 0)
max = Math.max(max, nums[i - j]);
}
dp[i] = nums[i] + max;
}
return dp[steps - 1];
}
全部评论
相关推荐
Java抽象小篮子:go批这招太狠了
点赞 评论 收藏
分享
05-29 15:00
广东金融学院 Java 每晚夜里独自颤抖:
你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享