Pregunta de entrevista de Amazon

Question based on stacks and then dynamic programming