soved problem 4... memoization