DP World Interview Question

4th question of leetcode contest