Amazon Interview Question

leetcode hard, graph problem, optimal not expected