Google Interview Question

DP problem