Pergunta de entrevista da empresa Amazon

Question based on stacks and then dynamic programming