added memoization and a test case