[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 A an integer array
     * @return a long integer
     */
    long long permutationIndex(vector<int>& A) {
        long long len = A.size();
        if(len < 2) return len;

        vector<long long> factorial(len, 1);
        for(long long i = len - 2;i >= 0;--i)
            factorial[i] = factorial[i + 1] * (len - 1 - i);

        long long res = 1;
        for(long long i = 0;i < len;++i){
            long long num = 0;
            for(long long j = i + 1;j < len;++j)
                if(A[j] < A[i]) ++num;
            res += num * factorial[i];
        }
        return res;
    }
};
时间: 2024-11-04 23:15:26

[LintCode] Permuation Index的相关文章

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大

LintCode &quot;Permutation Index II&quot; !

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

lintcode 容易题:Permutation Index 排列序号

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

Lintcode: Previous Permuation

Given a list of integers, which denote a permutation. Find the previous permutation in ascending order. Note The list may contains duplicate integers. Example For [1,3,2,3], the previous permutation is [1,2,3,3] For [1,2,3,4], the previous permutatio

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

[LintCode] Permutations

http://www.lintcode.com/en/problem/permutations/# Given a list of numbers, return all possible permutations. Example For nums = [1,2,3], the permutations are: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] class Solution { public: /** * @pa

lintcode 442实验Trieste

描述 实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法. 样例 思路 在了解字典树的性质和结构之后,就容易理解这次要求的是与之相似的三个功能:插入,查找,前缀查找. 插入操作: 建立结点pre,复制root.在pre的children[index]存放插入词汇word的第i个字符(用数字0到25表示a~z的26个字母,记作index),依次类推.若当前的children不存在,则建立大小为26的children结点数组.若children结点数组里的

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] First Position Unique Character

Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Example Given s = "lintcode", return 0. Given s = "lovelintcode", return 2. Solution 1. Two steps' iteration 1. first t