Skip to content

Commit d151a8d

Browse files
committed
Add solution #673
1 parent 289352a commit d151a8d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@
506506
670|[Maximum Swap](./0670-maximum-swap.js)|Medium|
507507
671|[Second Minimum Node In a Binary Tree](./0671-second-minimum-node-in-a-binary-tree.js)|Easy|
508508
672|[Bulb Switcher II](./0672-bulb-switcher-ii.js)|Medium|
509+
673|[Number of Longest Increasing Subsequence](./0673-number-of-longest-increasing-subsequence.js)|Medium|
509510
680|[Valid Palindrome II](./0680-valid-palindrome-ii.js)|Easy|
510511
684|[Redundant Connection](./0684-redundant-connection.js)|Medium|
511512
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 673. Number of Longest Increasing Subsequence
3+
* https://leetcode.com/problems/number-of-longest-increasing-subsequence/
4+
* Difficulty: Medium
5+
*
6+
* Given an integer array nums, return the number of longest increasing subsequences.
7+
*
8+
* Notice that the sequence has to be strictly increasing.
9+
*/
10+
11+
/**
12+
* @param {number[]} nums
13+
* @return {number}
14+
*/
15+
var findNumberOfLIS = function(nums) {
16+
const lengths = new Array(nums.length).fill(1);
17+
const counts = new Array(nums.length).fill(1);
18+
19+
for (let i = 1; i < nums.length; i++) {
20+
for (let j = 0; j < i; j++) {
21+
if (nums[i] > nums[j]) {
22+
if (lengths[j] + 1 > lengths[i]) {
23+
lengths[i] = lengths[j] + 1;
24+
counts[i] = counts[j];
25+
} else if (lengths[j] + 1 === lengths[i]) {
26+
counts[i] += counts[j];
27+
}
28+
}
29+
}
30+
}
31+
32+
const max = Math.max(...lengths);
33+
return lengths.reduce((sum, l, i) => {
34+
return sum + (l === max ? counts[i] : 0);
35+
}, 0);
36+
};

0 commit comments

Comments
 (0)