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 < ) return len; vector<long long> factorial(len, );
for(long long i = len - ;i >= ;--i)
factorial[i] = factorial[i + ] * (len - - i); long long res = ;
for(long long i = ;i < len;++i){
long long num = ;
for(long long j = i + ;j < len;++j)
if(A[j] < A[i]) ++num;
res += num * factorial[i];
}
return res;
}
};

[LintCode] Permuation Index的更多相关文章

  1. lintcode Permutation Index

    题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...

  2. Lintcode: Permutation Index II

    Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...

  3. Lintcode: Previous Permuation

    Given a list of integers, which denote a permutation. Find the previous permutation in ascending ord ...

  4. lintcode :Permutation Index 排列序号

    题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...

  5. * 197. Permutation Index【LintCode by java】

    Description Given a permutation which contains no repeated number, find its index in all the permuta ...

  6. 504. Inverted Index (Map Reduce) lintcode

    https://www.lintcode.com/problem/inverted-index-map-reduce/description -- decription of the map redu ...

  7. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  8. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  9. lintcode 75 Find Peak Element

    Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...

随机推荐

  1. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  2. 重写HashMap

    package gov.gfmis.fap.util; import java.util.ArrayList; import java.util.HashMap; import java.util.L ...

  3. hiho #1288 微软2016.4校招笔试题 Font Size

    #1288 : Font Size 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Steven loves reading book on his phone. The ...

  4. openCV的基本操作

    http://www.cnblogs.com/luluathena/archive/2010/09/29/1838471.html

  5. 反正切函数求圆周率 atan

    #define PI atan(1.0)*4 原理:tan ∏/4=1; atan2: 返回给定的 X 及 Y 坐标值的反正切值.反正切的角度值等于 X 轴正方向与通过原点和给定坐标点 (Y坐标, X ...

  6. HDOJ 1869

    #include<stdio.h> ][]; #define inf 0xffffff; void floyed(int n) { int i,j,k; ;k<n;k++) { ;i ...

  7. Longest Consecutive Sequence

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. C ...

  8. ios如何生成crash报告

    #include <signal.h> #include <execinfo.h> void OnProcessExceptionHandler(int sigl) { do ...

  9. Java中泛型在集合框架中的应用

    泛型是Java中的一个重要概念,上一篇文章我们说过,当元素存入集合时,集合会将元素转换为Object类型存储,当取出时也是按照Object取出的,所以用get方法取出时,我们会进行强制类型转换,并且通 ...

  10. Fastreport使用经验(转)在Delphi程序中访问报表对象

    Fastreport使用经验(转) 在Delphi程序中访问报表对象 最基本的方法就是frxReport1.FindObject. 然后把返回的对象强制转换成它的类型,当然,在报表中必须真的有这么个东 ...