dynamic programing

please convert it to iterative approach