solution implemented for the cacheMatrix assignment