Identity-based, last-value memoization

Worst-case time complexity is now O(n*n) again, but best case is
O(n*log(n)).