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. 修改织梦默认提示"dedecms提示信息!"

    在使用dedecms搜索的时候如果搜索频率过快,经常会跳出一个提示窗口提示"管理员设定搜索时间间隔为*秒,请稍后再试!".怎么自定义Dedecms提示信息呢?让心存不轨的家伙少一个 ...

  2. characterCustomezition的资源打包代码分析

    using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; class Creat ...

  3. myeclipse2014安装反编译插件

    一.在线安装方式: Eclipse Class Decompiler整合了目前最好的2个Java反编译工具Jad和JD-Core,并且和Eclipse Class Viewer无缝集成,能够很方便的使 ...

  4. users

    NAME users - print the user names of users currently logged in to the current host SYNOPSIS users [O ...

  5. js 基本介绍

    ecma  对象 三个包类型  String   ParseInt  ParseDouble instanceof typeof Math  对象 Array Date RegExp -- bom对象 ...

  6. php-fpm启动

    2014年6月30日 11:52:17 遇到一个问题,安装了redis.so后无论怎么重启nginx 还是 php-fpm都无法加载redis 最后发现重启php-fpm的参数弄错了 要这样: ./p ...

  7. Codebook model 视频抠像 xp sp3 + vs2005 + OpenCV 2.3.1

    Codebook model 视频抠像 xp sp3 + vs2005 + OpenCV 2.3.1 源码及详细文档下载 svn checkout http://cvg02.googlecode.co ...

  8. canva实践小实例 —— 马赛克效果

    前面给大家带来了操作像素的API,此时此刻,我觉得应该配以小实例来进行进一步的说明和演示,以便给大家带来更宽广的视野和灵感,你们看了我的那么多的文章,应该是懂我的风格,废话不多说,进入正题: 这次给大 ...

  9. Greedy:Protecting the Flowers(POJ 3262)

    保护花朵 题目大意:就是农夫有很多头牛在践踏花朵,这些牛每分钟破坏D朵花,农夫需要把这些牛一只一只运回去,这些牛各自离牛棚都有T的路程(有往返,而且往返的时候这只牛不会再破坏花),问怎么运才能使被践踏 ...

  10. jsp回车键登录代码

    $("body").keydown(function(event) { if (event.keyCode == "13") {// keyCode=13是回车 ...