Solution to one of the most well known dynamic programming problems