Dynamic Programming Pattern Leetcode 150 Problem