-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-111-maxIncreasingSubseq.js
69 lines (54 loc) · 1.75 KB
/
structy-111-maxIncreasingSubseq.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
68
69
// https://structy.net/problems/premium/max-increasing-subseq
// p: arr
// r: num+
// base cases:
// [1] => 1
// [a, b] => a < b: 2 // a >= b: 1
// [1, 2] => 2
// [1, 1, 1] => 1
// [1, 2, 2] => 2
// [2, 2, 1] => 1
// // recursion
// const numbers = [4, 18, 20, 10, 12, 15, 19]; // 4 10 12 15 19 -> 5
// 1 2 3 2 3 4 6
// iteration
const maxIncreasingSubseq = (numbers) => {
const firstNum = numbers[0];
const map = {};
map[firstNum] = 1;
for (let i = 1; i < numbers.length; i++) {
const num = numbers[i];
map[num] = findMax(map, num);
}
let max = -Infinity;
for (const key in map) {
max = Math.max(max, map[key]);
}
return max;
};
const findMax = (map, num) => {
let max = -Infinity;
for (let key in map) {
if (num >= +key) {
max = Math.max(map[key], max);
} else max = Math.max(max, 0);
}
if (max === map[num]) return max;
return max + 1;
};
// const numbers = [4, 18, 20, 10, 12, 15, 19]; // 4 10 12 15 19 -> 5
// 4 1 2 3 2 3 4 6
// 18 0 2 3
// f(0) - f(i-1):
// f(i) = f(i-j) + 1
// const numbers = [12, 9, 2, 5, 4, 32, 90, 20]; // 2 4 32 90 -> 4
// 1 1 2 2 2 3 4
const numbers = [42, 50, 51, 60, 55, 70, 4, 5, 70]; // -> 5
// 1 2 3 4 4 5 1 2 6
// const numbers = [42, 50, 51, 60, 55, 70, 4, 5, 60]; // -> 5
// // 1 2 3 4 4 5 1 2 5
// const numbers = [42, 50, 51, 60, 55, 55, 4, 5, 60]; // -> 5
// // 1 2 3 4 4 4 1 2 5
// const numbers = [42, 50, 60, 51, 55, 4, 5, 60]; // -> 5
// // 1 2 3 3 4 1 2 5
console.log(maxIncreasingSubseq(numbers));