Question d’entretien chez Microsoft

HackerRank Hard question. Solved it with dynamic programming.