Amazon Interview Question

Leetcode hard question, about different algorithms