-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-111-maxIncreasingSubseq-2.js
179 lines (147 loc) · 4.45 KB
/
structy-111-maxIncreasingSubseq-2.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// i
// 12, 9, 2, 5, 4, 32, 90, 20 90
// j
// 1 1 1 2 2 3 4 3 4
//
// iteration O(n^2) O(n)
const maxIncreasingSubseq = (nums) => {
const arr = nums.map((num) => 1);
for (let i = 1; i < nums.length; i++) {
for (let j = 0; j < i; j++) {
console.log(i, j);
nums[j] < nums[i] && arr[j] >= arr[i] && (arr[i] = arr[j] + 1);
}
}
return Math.max(...arr);
};
// p: arr
// r: num
// [4]
// 1
// [4, 18] [18, 4]
// 1 2 1 1
// [4, 28, 20]
// 1 2 2
// i
// [4, 18, 20, 10, 12, 15, 19]; => 5
// 1 2 3 2
// j
// i
// [12, 9, 2, 5, 4, 32, 90, 20]
// 1 1 1 2 2 3 4 4
// j
// const maxIncreasingSubseq = (nums) => {
// const map = {};
// map[nums[0]] = 1;
// for (let i = 1; i < nums.length; i++) {
// let max = -Infinity;
// // i
// // nums [4, 18, 20, 10, 12, 15, 19]
// // map keys 4
// // map vals 1
// // max 1
// for (let key in map) {
// if (nums[i] >= +key) {
// max = Math.max(map[key], max);
// } else max = Math.max(max, 0);
// }
// max !== map[nums[i]] && //////////////////////
// (map[nums[i]] = max + 1);
// }
// let max = -Infinity;
// for (const key in map) {
// max = Math.max(max, map[key]);
// }
// return max;
// };
// console.log(maxIncreasingSubseq([4, 18, 20, 10, 12, 15, 19]));
// console.log(maxIncreasingSubseq([12, 9, 2, 5, 4, 32, 90, 20]));
// {
// 4 : 1
// 18: 2
// 20: 3
// 10: 2
// 12: 3
//
// }
// [4, 18, 20, 10, 12, 3, 15, 19]
// //////////////////
// const maxIncreasingSubseq = (numbers) => {
// const map = {}; // arr[1,...] not working bcause of [42, 50, 51, 60, 55, 70, 4, 5, 70]
// map[numbers[0]] = 1;
// for (let i = 1; i < numbers.length; i++) {
// let max = -Infinity;
// let num = numbers[i];
// for (let key in map) {
// // if num >= key: max = the greatest number next to num
// if (num >= +key) {
// max = Math.max(max, map[key]);
// } else {
// // if num < key:
// //// if num < all keys: max = 0;
// //// if num < some keys: max = max of key before the current key
// max = Math.max(max, 0);
// }
// }
// // [1, 2, 3, 4, 4, 5, 1, 2, 6];
// // if key === max -> keep max, [42, 50, 51, 60, 55, 70, 4, 5, 70] else:
// max !== map[num] && (map[num] = max + 1);
// }
// let max = -Infinity;
// for (let key in map) {
// max = Math.max(max, map[key]);
// }
// return max;
// };
// console.log(maxIncreasingSubseq([42, 50, 51, 60, 55, 70, 4, 5, 70]));
// [1, 2, 3, 4, 4, 5, 1, 2, 6];
console.log(maxIncreasingSubseq([4, 18, 20, 10, 12, 15, 19]));
// [1, 2, 3, 2, 3, 4, 6];
// { '4': 1, '10': 1, '12': 1, '15': 1, '18': 2, '19': 1, '20': 3 }
// console.log(maxIncreasingSubseq([12, 9, 2, 5, 4, 32, 90, 20]));
// [1, -, -, -, -, 6, 7, 6];
// console.log(
// maxIncreasingSubseq([
// 1, 2, 3, 4, 5, 12, 6, 30, 7, 8, 9, 10, 11, 12, 13, 10, 18, 14, 15, 16, 17,
// 18, 19, 20, 21, 100, 104,
// ])
// );
// [4, 18, 20, 10, 12, 15, 19]
// 1 2 3 2
//
// i
// 1, 2, 3, 4, 5, 12, 6, 30, 7, 8, 9, 10, 11, 12, 13, 10, 18, 14, 15, 16, 17, 18, 19, 20, 21, 100, 104, // -> 23
//
// 1, 2, 3, 4, 5, 6, 6, 7, 7, 8, 9, 10, 11, 12, 13, 10, 11, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
// j 14 14
// not working
// const maxIncreasingSubseq = (numbers) => {
// // const arr = [1];
// const map = {};
// map[numbers[0]] = 1;
// for (let i = 1; i < numbers.length; i++) {
// // arr[i] = 1;
// for (let j = 0; j < i; j++) {
// numbers[i] > numbers[j] &&
// (map[numbers[i]] =
// Math.max(map[numbers[i]], map[numbers[j]]) || map[numbers[j]] + 1); //
// }
// }
// // return Math.max(...arr);
// let max = -Infinity;
// for (const key in map) {
// max = Math.max(max, map[key]);
// }
// return max;
// };
// not working
// const maxIncreasingSubseq = (numbers) => {
// const arr = [1];
// for (let i = 1; i < numbers.length; i++) {
// arr[i] = 1;
// for (let j = 0; j < i; j++) {
// numbers[i] > numbers[j] && (arr[i] = arr[j] + 1);
// }
// }
// return Math.max(...arr);
// };