Check if the result of this recursive call is memoised already , if it is then just return the previously calculated result
if ( dp [ val ] [ idx ] [ prev ] != - 1 ) : NEW_LINE INDENT return dp [ val ] [ idx ] [ prev ] NEW_LINE DEDENT