Dynamic Programming Pattern Leetcode 1501 Record