Amazon Interview Question

leetcode, leetcode hard optimal not required