lintcode197. Permutation Index

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;
    }
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容