Pergunta de entrevista da empresa Amazon

Graph question with DP problem