GlobalLogic interview question

Question from dynamic programming and data structure