Dynamic Programming Pattern Leetcode Javascript Solution