Add cache for recursive solution