memoized part implemented. time complexity tests written.