Longest Common Subsequence from reverse
let dpr = new Array ( N + 2 ) ; for ( let i = 0 ; i < N + 2 ; i ++ ) { dpr [ i ] = new Array ( M + 2 ) ; for ( let j = 0 ; j < M + 2 ; j ++ ) dpr [ i ] [ j ] = 0 ; } for ( let i = N ; i >= 1 ; i -- ) { for ( let j = M ; j >= 1 ; j -- ) { if ( A [ i - 1 ] == B [ j - 1 ] ) dpr [ i ] [ j ] = dpr [ i + 1 ] [ j + 1 ] + 1 ; else dpr [ i ] [ j ] = Math . max ( dpr [ i + 1 ] [ j ] , dpr [ i ] [ j + 1 ] ) ; } }