Skip to content

Commit c161a43

Browse files
authored
Create 3177-find-the-maximum-length-of-a-good-subsequence-ii.js
1 parent e2b9b27 commit c161a43

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const maximumLength = function(nums, k) {
7+
const n = nums.length;
8+
const res = Array(k + 1).fill(0);
9+
const dp = Array.from({ length: k + 1 }, () => new Map());
10+
for (const a of nums) {
11+
for (let i = k; i >= 0; i--) {
12+
const v = dp[i].get(a) || 0;
13+
const vv = Math.max(v + 1, i > 0 ? res[i - 1] + 1 : 0)
14+
dp[i].set(a, vv);
15+
res[i] = Math.max(res[i], vv);
16+
}
17+
}
18+
return res[k]
19+
};

0 commit comments

Comments
 (0)