Amazon interview question

Just a leetcode medium/hard problem