TuSimple Interview Question

some algorithm question, leedcode medium-hard level