LintCode "Permutation Index II" !

Simply a variation to "Permutation Index". When calculating current digit index, we consider duplicated case.

Again, similar as "Digit Counts", it is another counting problem and stil digit by digit.

And please note: we can use Fenwick Tree to calculate rank by O(nlgn)

class Solution
{
    long long dupPerm(unordered_map<int, int> &hash)
    {
        if (hash.empty()) return 1;

        long long dup = 1;
        for (auto it = hash.begin(); it != hash.end(); ++it)
        {
        dup *= w(it->second);
        }

        return dup;
    }
    long long w(int n) // n*(n-1)*..*2*1
    {
        long long ret = 1;
        while(n > 1)
        {
            ret *= n;
            n --;
        }
        return ret;
    }
public:
    /**
     * @param A an integer array
     * @return a long integer
     */
    long long permutationIndexII(vector<int>& A)
    {
        int n = A.size();
        if(!n) return 0;

        long long index = 1;
        long long factor= w(n - 1);

        //  MSB -> LSB
        for(int i = 0; i < n; i ++)
        {
            // Calc rank
          int rank = 0;
          for(int j = i + 1; j < n; j ++)
          {
            if(A[i] > A[j])
              rank ++;
          }

          // Calc dup factor
            unordered_map<int, int> hm;
            for(int j = i; j < n; j ++)
                ++hm[A[j]];

            index += rank * factor / dupPerm(hm);
            if(i < (n - 1))factor /= n - i - 1;
        }

        return index;
    }
};
时间: 2024-10-12 22:18:43

LintCode "Permutation Index II" !的相关文章

Lintcode: Permutation Index

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. 在计算最终的 index 时需要动态计算某个数的相对大小.我们可通过两重循环得出到某个索引处值的相对大小. 正确 以4,1,2为例,4为第3大

Permutation Index I &amp; II

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/

lintcode 容易题:Permutation Index 排列序号

题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有求出所有的排列,然后找出其对应的下标,但是怎么求出排列,在做Project Euler 时候碰到过,但是现在我又不会写了,那时候毕竟是抄别人的程序的.在geekviewpoint看到一种很厉害的解法,不需要求所有的排列,直接根据给的数组进行求解. 思路: 1.对于四位数:4213 = 4*100+2

Lintcode: Sort Colors II 解题报告

Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k. 注意

Lintcode: Majority Number II 解题报告

Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra

Lintcode: k Sum II

Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target. Example Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions. 这道题同Combination Sum II 1 public class Solution { 2 /** 3

[LintCode] Permuation Index

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. class Solution { public: /** * @param

Palindrome Permutation I &amp; II

Palindrome Permutation I Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even

[LeetCode] Palindrome Permutation I &amp; II

Palindrome Permutation Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even le