Dynamic Programming Pattern Leetcode Two