Number of Longest Increasing Subsequences | Function to count the number of LIS in the array nums [ ] ; Base Case ; Initialize dp_l array with 1 s ; Initialize dp_c array with 1 s ; If current element is smaller ; Store the maximum element from dp_l ; Stores the count of LIS ; Traverse dp_l and dp_c simultaneously ; Update the count ; Return the count of LIS ; Given array arr [ ] ; Function Call
function findNumberOfLIS ( nums ) { if ( nums . length == 0 ) return 0 ; var n = nums . length ; var dp_l = Array ( n ) . fill ( 1 ) ; var dp_c = Array ( n ) . fill ( 1 ) ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = 0 ; j < i ; j ++ ) { if ( nums [ i ] <= nums [ j ] ) continue ; if ( dp_l [ j ] + 1 > dp_l [ i ] ) { dp_l [ i ] = dp_l [ j ] + 1 ; dp_c [ i ] = dp_c [ j ] ; } else if ( dp_l [ j ] + 1 == dp_l [ i ] ) dp_c [ i ] += dp_c [ j ] ; } } var max_length = 0 ; dp_l . forEach ( i => { max_length = Math . max ( i , max_length ) ; } ) ; var count = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( dp_l [ i ] == max_length ) count += dp_c [ i ] ; } return count ; } var arr = [ 1 , 3 , 5 , 4 , 7 ] ; document . write ( findNumberOfLIS ( arr ) ) ;