Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.
Example
Given [1,2,4], return 1.
分析:http://www.cnblogs.com/EdwardLiu/p/5104310.html
以4,1,2为例,4为第3大数,1为剩余序列第1大数,2为剩余序列第1大数,
故表达式为:2*2! + 0*1! + 0*0! + 1 = 5
以2,4,1为例,2为第2大数,4为剩余序列第2大数,1为剩余序列第1大数
故表达式为:1*2! + 1*1! + 0*0! + 1 = 4
这后面这个1一定要加,因为前面算的都是比该数小的数,加上这个1,才是该数是第几大数。
对于2*2!,2!表示当时当前位后面还有两位,全排列有2!种, 第一个2表示比4小的有两个数。全排列可以以它们开头。
1 public class Solution { 2 public long permutationIndex(int[] A) { 3 long index = 0, fact = 1; 4 for (int i = A.length - 1; i >= 0; i--) { 5 int numOfSmaller = 0; 6 for (int j = i + 1; j < A.length; j++) { 7 if (A[j] < A[i]) numOfSmaller++; // numOfSmaller refers to the numbers which can begin with; 8 } 9 index += numOfSmaller * fact; 10 fact *= (A.length - i); 11 } 12 return index + 1; 13 } 14 }
Permutation Index II
Given a permutation which may contain repeated numbers, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.
Example
Given the permutation [1, 4, 2, 2]
, return 3
.
分析:https://segmentfault.com/a/1190000004683277
与上一题的不同之处时会有重复的数。那么,只要在发现是重复数的那一位用numOfSmallers* fact
的结果除以重复的次数dup
再加入index就可以了。当然,每个重复数的dup都要阶乘,例如有3个2,4个8,dup
就是3! * 4! = 144
。index
是所有previous排列的次数和,返回下一次index+1
。
1 import java.util.HashMap; 2 3 public class Solution { 4 public long permutationIndexII(int[] A) { 5 long index = 0, fact = 1, dup = 1; 6 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); 7 for (int i = A.length - 1; i >= 0; i--) { 8 if (!map.containsKey(A[i])) { 9 map.put(A[i], 1); 10 } else { 11 map.put(A[i], map.get(A[i]) + 1); 12 dup *= map.get(A[i]); 13 } 14 int numOfSmallers = 0; 15 for (int j = i + 1; j < A.length; j++) { 16 if (A[j] < A[i]) 17 numOfSmallers++; 18 } 19 index += numOfSmallers * fact / dup; 20 fact *= (A.length - i); 21 } 22 return index + 1; 23 } 24 }