Number of Longest Increasing Subsequence LeetCode solution in JavaScript

1
var findNumberOfLIS = function (nums) {
2
const { length } = nums;
3
const dpLength = Array(length).fill(1);
4
const dpCount = Array(length).fill(1);
5

6
for (let right = 0; right < length; right++) {
7
for (let left = 0; left < right; left++) {
8
if (nums[left] >= nums[right]) continue;
9
if (dpLength[left] + 1 === dpLength[right]) {
10
dpCount[right] += dpCount[left];
11
} else if (dpLength[left] + 1 > dpLength[right]) {
12
dpLength[right] = dpLength[left] + 1;
13
dpCount[right] = dpCount[left];
14
}
15
}
16
}
17

18
const maxLength = Math.max(...dpLength);
19
return dpLength.reduce((result, length, index) => {
20
const count = dpCount[index];
21
return result + (maxLength === length ? count : 0);
22
}, 0);
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0