@weidairpi Thanks for the response. OK, I see your point. It makes sense. The interesting thing is that if I remove the cnt, leetcode shows the performance beats 57%. If there is cnt, the performance beats 97%. Huge difference! Before I wonder whether there is some very tricky stuff which the cnt brings in.
What's the time complexity of your solution?
I made a rough analysis of the time complexity:
Overall time complexity = O(k*n!).
Because to build each bucket, the first level recursion has n options, the second level has n - 1 options, the third level has n - 2 options, ..... So to build one bucket takes O(n!) time complexity, and there are k buckets, the overall time complexity = O(k*n!).
But the real time complexity must be more tight than the above. Because the element used by one bucket will not be re-used by another.
I appreciate that you can share your analysis about the time complexity of your solution.