if already calculated the value return the val instead of calculating again
if ( dp [ col ] [ prevCol ] [ painted ] [ prev ] != - 1 ) : NEW_LINE INDENT return dp [ col ] [ prevCol ] [ painted ] [ prev ] NEW_LINE DEDENT res = 0 NEW_LINE