File tree 1 file changed +13
-12
lines changed
scripts/algorithms/L/Longest Palindromic Subsequence
1 file changed +13
-12
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 414 ms (Top 33.56%) | Memory: 88.6 MB (Top 56.51%)
1
2
var longestPalindromeSubseq = function ( s ) {
2
- const { length } = s ;
3
- const dp = Array ( length ) . fill ( '' ) . map ( ( ) => Array ( length ) . fill ( 0 ) ) ;
3
+ const { length } = s ;
4
+ const dp = Array ( length ) . fill ( '' ) . map ( ( ) => Array ( length ) . fill ( 0 ) ) ;
4
5
5
- for ( let start = 0 ; start < length ; start ++ ) {
6
- const str = s [ start ] ;
7
- dp [ start ] [ start ] = 1 ;
6
+ for ( let start = 0 ; start < length ; start ++ ) {
7
+ const str = s [ start ] ;
8
+ dp [ start ] [ start ] = 1 ;
8
9
9
- for ( let end = start - 1 ; end >= 0 ; end -- ) {
10
- dp [ start ] [ end ] = str === s [ end ]
11
- ? dp [ start - 1 ] [ end + 1 ] + 2
12
- : Math . max ( dp [ start - 1 ] [ end ] , dp [ start ] [ end + 1 ] )
13
- }
14
- }
15
- return dp [ length - 1 ] [ 0 ] ;
10
+ for ( let end = start - 1 ; end >= 0 ; end -- ) {
11
+ dp [ start ] [ end ] = str === s [ end ]
12
+ ? dp [ start - 1 ] [ end + 1 ] + 2
13
+ : Math . max ( dp [ start - 1 ] [ end ] , dp [ start ] [ end + 1 ] )
14
+ }
15
+ }
16
+ return dp [ length - 1 ] [ 0 ] ;
16
17
} ;
You can’t perform that action at this time.
0 commit comments