Amazon interview question

solve a leet code question