Permutation Index II

Question

Problem Statement

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.

题解

Permutation Index 的扩展,这里需要考虑重复元素,有无重复元素最大的区别在于原来的1!, 2!, 3!...等需要除以重复元素个数的阶乘,颇有点高中排列组合题的味道。记录重复元素个数同样需要动态更新,引入哈希表这个万能的工具较为方便。

Python

  1. class Solution:
  2. # @param {int[]} A an integer array
  3. # @return {long} a long integer
  4. def permutationIndexII(self, A):
  5. if A is None or len(A) == 0:
  6. return 0
  7. index = 1
  8. factor = 1
  9. for i in xrange(len(A) - 1, -1, -1):
  10. hash_map = {A[i]: 1}
  11. rank = 0
  12. for j in xrange(i + 1, len(A)):
  13. if A[j] in hash_map.keys():
  14. hash_map[A[j]] += 1
  15. else:
  16. hash_map[A[j]] = 1
  17. # get rank
  18. if A[i] > A[j]:
  19. rank += 1
  20. index += rank * factor / self.dupPerm(hash_map)
  21. factor *= (len(A) - i)
  22. return index
  23. def dupPerm(self, hash_map):
  24. if hash_map is None or len(hash_map) == 0:
  25. return 0
  26. dup = 1
  27. for val in hash_map.values():
  28. dup *= self.factorial(val)
  29. return dup
  30. def factorial(self, n):
  31. r = 1
  32. for i in xrange(1, n + 1):
  33. r *= i
  34. return r

C++

  1. class Solution {
  2. public:
  3. /**
  4. * @param A an integer array
  5. * @return a long integer
  6. */
  7. long long permutationIndexII(vector<int>& A) {
  8. if (A.empty()) return 0;
  9. long long index = 1;
  10. long long factor = 1;
  11. for (int i = A.size() - 1; i >= 0; --i) {
  12. int rank = 0;
  13. unordered_map<int, int> hash;
  14. ++hash[A[i]];
  15. for (int j = i + 1; j < A.size(); ++j) {
  16. ++hash[A[j]];
  17. if (A[i] > A[j]) {
  18. ++rank;
  19. }
  20. }
  21. index += rank * factor / dupPerm(hash);
  22. factor *= (A.size() - i);
  23. }
  24. return index;
  25. }
  26. private:
  27. long long dupPerm(unordered_map<int, int> hash) {
  28. if (hash.empty()) return 1;
  29. long long dup = 1;
  30. for (auto it = hash.begin(); it != hash.end(); ++it) {
  31. dup *= fact(it->second);
  32. }
  33. return dup;
  34. }
  35. long long fact(int num) {
  36. long long val = 1;
  37. for (int i = 1; i <= num; ++i) {
  38. val *= i;
  39. }
  40. return val;
  41. }
  42. };

Java

  1. public class Solution {
  2. /**
  3. * @param A an integer array
  4. * @return a long integer
  5. */
  6. public long permutationIndexII(int[] A) {
  7. if (A == null || A.length == 0) return 0L;
  8. Map<Integer, Integer> hashmap = new HashMap<Integer, Integer>();
  9. long index = 1, fact = 1, multiFact = 1;
  10. for (int i = A.length - 1; i >= 0; i--) {
  11. // collect its repeated times and update multiFact
  12. if (hashmap.containsKey(A[i])) {
  13. hashmap.put(A[i], hashmap.get(A[i]) + 1);
  14. multiFact *= hashmap.get(A[i]);
  15. } else {
  16. hashmap.put(A[i], 1);
  17. }
  18. // get rank every turns
  19. int rank = 0;
  20. for (int j = i + 1; j < A.length; j++) {
  21. if (A[i] > A[j]) rank++;
  22. }
  23. // do divide by multiFact
  24. index += rank * fact / multiFact;
  25. fact *= (A.length - i);
  26. }
  27. return index;
  28. }
  29. }

源码分析

在计算重复元素个数的阶乘时需要注意更新multiFact的值即可,不必每次都去计算哈希表中的值。对元素A[i]需要加入哈希表 - hash.put(A[i], 1);,设想一下2, 2, 1, 1的计算即可知。

复杂度分析

双重 for 循环,时间复杂度为 O(n^2), 使用了哈希表,空间复杂度为 O(n).