-
Notifications
You must be signed in to change notification settings - Fork 0
/
BOJ11055.js
67 lines (49 loc) · 1.31 KB
/
BOJ11055.js
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
// Solution 1
function solution1(n, nums) {
const dp = Array.from({ length: n }, () => 0);
for (let i = 0; i < n; i++) {
const values = [nums[i]];
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
values.push(dp[j] + nums[i]);
}
}
dp[i] = Math.max(...values);
}
const maxSum = Math.max(...dp);
return maxSum;
}
// Solution 2
function solution2(n, nums) {
const dp = Array.from({ length: n }, () => 0);
for (let i = 0; i < n; i++) {
dp[i] = nums[i];
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + nums[i]);
}
}
}
const maxSum = Math.max(...dp);
return maxSum;
}
// Solution 3
function solution3(n, nums) {
const dp = Array.from({ length: n }, (_, index) => nums[index]);
for (let i = 0; i < n; i++) {
for (let j = 0; j < i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j] + nums[i]);
}
}
}
const maxSum = Math.max(...dp);
return maxSum;
}
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const n = parseInt(input[0]);
const nums = input[1].split(' ').map((value) => parseInt(value));
console.log(solution1(n, nums));
console.log(solution2(n, nums));
console.log(solution3(n, nums));