Shopee interview question

Solve a problem with backtracking