A faster approach to the same problem.