美文网首页
lintcode197. Permutation Index

lintcode197. Permutation Index

作者: 刘小小gogo | 来源:发表于2018-08-25 15:50 被阅读0次
image.png image.png

https://www.cnblogs.com/kittyamin/p/5014947.html

class Solution {
public:
    /**
     * @param A: An array of integers
     * @return: A long integer
     */
    long long permutationIndex(vector<int> &A) {
        // write your code here
        long long index = 0;
        for(int i = 0; i < A.size(); i++){
            int count = 0;
            for(int j = i + 1; j < A.size(); j++){
                if(A[j] < A[i]) count++;
            }
            long long factor = 1;
            if(count){
                int n = A.size() - i - 1;
                cout<<n<<endl;
                while(n > 0){
                    factor *= n--;
                }
            }

            index += count * factor;
        }
        return index + 1;
    }
};

相关文章

网友评论

      本文标题:lintcode197. Permutation Index

      本文链接:https://www.haomeiwen.com/subject/cadgiftx.html