#198 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.

题目思路:

这题和#197不同点在于array中可能有重复元素。高中数学概率告诉我们,有重复元素的情况下,重复k个就要从排列组合总数中除以k!。这里我用一个map来记录每个元素和它出现的个数,如果map中某个数对应的个数>1,就在所求得的base*order中除以个数的阶乘(对于每个重复元素都要这么操作)。

Mycode(AC = 16ms):

class Solution {
public:
    /**
     * @param A an integer array
     * @return a long integer
     */
    long long permutationIndexII(vector<int>& A) {
        // Write your code here
        return permutationIndex(A, 0) + 1;
    }
    
    long long permutationIndex(vector<int>& A, int idx) {
        if (idx >= A.size()) {
            return 0;
        }
        
        map<int, int> helper;
        helper[A[idx]] = 1;
        
        // find A[idx] is x-th smallest in the index idx~end
        long long order = 0;
        for (int i = idx + 1; i < A.size(); i++) {
            if (helper.find(A[i]) != helper.end()) {
                helper[A[i]]++;
            }
            else {
                helper[A[i]] = 1;
            }
            
            if (A[i] < A[idx]) {
                order++;
            }
        }
        
        // find the number of permutation for A[idx + 1..end]
        long long base = 1;
        for (int i = 1; i < A.size() - idx; i++) {
            base *= (long long)i;
        }
        
        // if there is repeat numbers, then reduce the order by factor
        int factor = 1;
        for (auto it = helper.begin(); it != helper.end(); it++) {
            if (it->second > 1) {
                factor *= factorial(it->second);
            }
        }
        
        return base * order/factor + permutationIndex(A, idx + 1);
    }
    
    long long factorial(int num) {
        long long f = 1;
        for (int i = 1; i <= num; i++) {
            f *= (long long)i;
        }
        return f;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值