- 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
- 36
- 37
- 38
- 39
- 40
- 41
java解法
- 解题思路

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
int[] vals = new int[input.length];
for (int i = 0; i < input.length; i++) {
vals[i] = Integer.parseInt(input[i]);
}
System.out.println(findMax(vals));
}
public static int maxPoints(int[] arr) {
int n = arr.length;
if (n == 1) {
return arr[0];
}
int[] p = new int[n];
p[0] = arr[0];
if (n > 1) {
p[1] = Math.max(arr[0], arr[1]);
}
for (int i = 2; i < n; i++) {
p[i] = Math.max(p[i - 1], p[i - 2] + arr[i]);
}
return p[n - 1];
}
public static int findMax(int[] vals) {
int n = vals.length;
if (n == 1) {
return vals[0];
}
int[] vals1 = new int[n - 1];
int[] vals2 = new int[n - 1];
System.arraycopy(vals, 0, vals1, 0, n - 1);
System.arraycopy(vals, 1, vals2, 0, n - 1);
return Math.max(maxPoints(vals1), maxPoints(vals2));
}
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
C++解法
更新中
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
C解法
更新中
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
JS解法
-
解题思路
-

const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", (line) => {
const nums = line.split(" ").map(Number);
console.log(computeMaxScore(nums));
});
function computeMaxScore(nums) {
const n = nums.length;
if (n === 1) return nums[0];
return Math.max(calcMax(nums.slice(0, -1)), calcMax(nums.slice(1)));
}
function calcMax(arr) {
const memo = new Array(arr.length).fill(-1);
return maxWithMemo(arr, arr.length - 1, memo);
}
function maxWithMemo(arr, i, memo) {
if (i < 0) return 0;
if (memo[i] !== -1) return memo[i];
memo[i] = Math.max(
maxWithMemo(arr, i - 1, memo),
maxWithMemo(arr, i - 2, memo) + arr[i]
);
return memo[i];
}
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
- 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
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
注意:
如果发现代码有用例覆盖不到的情况,欢迎反馈!会在第一时间修正,更新。
解题不易,如对您有帮助,欢迎点赞/收藏
评论记录:
回复评论: