-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1911-maximum-alternating-subsequence-sum.js
More file actions
53 lines (42 loc) · 1.3 KB
/
Copy path1911-maximum-alternating-subsequence-sum.js
File metadata and controls
53 lines (42 loc) · 1.3 KB
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
var maxAlternatingSum = function (nums) {
return maxAlternatingSumBottomTop(nums);
}
var maxAlternatingSumBottomTop = function (nums) {
let even = 0, odd = 0;
for (let i = nums.length -1; i >= 0; i--) {
const tmpEven = Math.max(odd + nums[i], even);
const tmpOdd = Math.max(even - nums[i], odd);
[even, odd] = [tmpEven, tmpOdd];
}
return even;
}
var maxAlternatingSumTopBottom = function (nums) {
const dp = {};
const dfs = (i, even) => {
const dpKey = even ? 'even' : 'odd';
if (i === nums.length) return 0;
if (dp[i] && dp[i][dpKey]) return dp[i][dpKey];
if (!dp[i]) dp[i] = {};
const num = even ? nums[i] : (-1 * nums[i]),
useThisItem = num + dfs(i + 1, !even),
skipThisItem = dfs(i + 1, even);
dp[i][dpKey] = Math.max(useThisItem, skipThisItem);
return dp[i][dpKey];
}
const result = dfs(0, true);
return result;
};
const data = [
{ nums: [3,2,9,2,10], output: 18 },
{ nums: [2, 1, 5, 4, 4], output: 6 },
{ nums: [4, 2, 5, 3], output: 7 },
{ nums: [5, 6, 7, 8], output: 8 },
{ nums: [6,2,1,2,4,5], output: 10 }
];
for (let d of data) {
console.log(JSON.stringify(d));
const result = maxAlternatingSum(d.nums);
console.log('result = ', result);
console.log('ok = ', result === d.output);
console.log('----------');
}