File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 506
506
670|[ Maximum Swap] ( ./0670-maximum-swap.js ) |Medium|
507
507
671|[ Second Minimum Node In a Binary Tree] ( ./0671-second-minimum-node-in-a-binary-tree.js ) |Easy|
508
508
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|
509
510
680|[ Valid Palindrome II] ( ./0680-valid-palindrome-ii.js ) |Easy|
510
511
684|[ Redundant Connection] ( ./0684-redundant-connection.js ) |Medium|
511
512
686|[ Repeated String Match] ( ./0686-repeated-string-match.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments