Domanda di colloquio di Microsoft

HackerRank Hard question. Solved it with dynamic programming.