Find smallest permutation under constraints | Airbnb Coding Question