## Friday, April 15, 2016

### Lintcode 198 - Permutation Index II

http://www.cnblogs.com/EdwardLiu/p/5104409.html
```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.```

``` 6     public long permutationIndexII(int[] A) {
7         // Write your code here
8         if (A==null || A.length==0) return new Long(0);
9         int len = A.length;
10         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11         long index = 0, fact = 1, mulFact = 1;
12         for (int i=len-1; i>=0; i--) {
13             if (!map.containsKey(A[i])) {
14                 map.put(A[i], 1);
15             }
16             else {
17                 map.put(A[i], map.get(A[i])+1);
18                 mulFact *= map.get(A[i]);
19             }
20             int count = 0;
21             for (int j=i+1; j<len; j++) {
22                 if (A[i] > A[j]) count++;
23             }
24             index += count*fact/mulFact;
25             fact *= (len-i);
26         }
27         index = index + 1;
28         return index;
29     }```
http://blog.csdn.net/earthma/article/details/47074779

C(n-1) = (首元素为小于当前元素，之后的全排列值)
P(1) = 1;

C(n-1) = (n-1)!/(A1!*A2!*···*Aj!)*k(其中Ai 为重复元素的个数，k为小于首元素前不重复的个数)

http://algorithm.yuanbin.me/zh-hans/exhaustive_search/permutation_index_ii.html

• 在loop中每遇到一个元素，都要重新建立一个hashmap来计算
• 元素自己也要先加入到hashmap中，不然在右边计算重复元素的时候就可能少掉自己的一次

``````    public long permutationIndexII(int[] A) {
if (A == null || A.length == 0) return 0L;

Map<Integer, Integer> hashmap = new HashMap<Integer, Integer>();
long index = 1, fact = 1, multiFact = 1;
for (int i = A.length - 1; i >= 0; i--) {
// collect its repeated times and update multiFact
if (hashmap.containsKey(A[i])) {
hashmap.put(A[i], hashmap.get(A[i]) + 1);
multiFact *= hashmap.get(A[i]);
} else {
hashmap.put(A[i], 1);
}
// get rank every turns
int rank = 0;
for (int j = i + 1; j < A.length; j++) {
if (A[i] > A[j]) rank++;
}
// do divide by multiFact
index += rank * fact / multiFact;
fact *= (A.length - i);
}

return index;
}``````

```    long fac(int numerator) {

long now = 1;
for (int i = 1; i <= numerator; i++) {
now *= (long) i;
}
return now;
}
long generateNum(HashMap<Integer, Integer> hash) {
long denominator = 1;
int sum = 0;
for (int val : hash.values()) {
if(val == 0 )
continue;
denominator *= fac(val);
sum += val;
}
if(sum==0) {
return sum;
}
return fac(sum) / denominator;
}

public long permutationIndexII(int[] A) {
HashMap<Integer, Integer> hash = new HashMap<Integer, Integer>();

for (int i = 0; i < A.length; i++) {
if (hash.containsKey(A[i]))
hash.put(A[i], hash.get(A[i]) + 1);
else {
hash.put(A[i], 1);
}
}
long ans = 0;
for (int i = 0; i < A.length; i++) {
HashMap<Integer, Integer> flag = new HashMap<Integer, Integer>();

for (int j = i + 1; j < A.length; j++) {
if (A[j] < A[i] && !flag.containsKey(A[j])) {
flag.put(A[j], 1);

hash.put(A[j], hash.get(A[j])-1);
ans += generateNum(hash);
hash.put(A[j], hash.get(A[j])+1);

}

}
hash.put(A[i], hash.get(A[i])-1);
}

return ans+1;

}```