Finding the index of a given permutation

前端 未结 7 558
闹比i
闹比i 2020-12-10 15:38

I\'m reading the numbers 0, 1, ..., (N - 1) one by one in some order. My goal is to find the lexicography index of this given permutation, using only O(1)

7条回答
  •  情深已故
    2020-12-10 16:16

    There is a java solution to this problem on geekviewpoint. It has a good explanation for why it's true and the code is easy to follow. http://www.geekviewpoint.com/java/numbers/permutation_index. It also has a unit test that runs the code with different inputs.

提交回复
热议问题