Amazon interview question

Leetcode hard question, about different algorithms