added method 2 using DP