Update dp as the maximum value
dp [ i + 1 ] [ h1 ] = Math . max ( dp [ i + 1 ] [ h1 ] , dp [ i ] [ j ] + ( ( h1 >= l && h1 <= r ) ? 1 : 0 ) ) ; dp [ i + 1 ] [ h2 ] = Math . max ( dp [ i + 1 ] [ h2 ] , dp [ i ] [ j ] + ( ( h2 >= l && h2 <= r ) ? 1 : 0 ) ) ; } } } let ans = 0 ;