[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.
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的更多相关文章
- lintcode Permutation Index
题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...
- Lintcode: Permutation Index II
Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...
- Lintcode: Previous Permuation
Given a list of integers, which denote a permutation. Find the previous permutation in ascending ord ...
- lintcode :Permutation Index 排列序号
题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有 ...
- * 197. Permutation Index【LintCode by java】
Description Given a permutation which contains no repeated number, find its index in all the permuta ...
- 504. Inverted Index (Map Reduce) lintcode
https://www.lintcode.com/problem/inverted-index-map-reduce/description -- decription of the map redu ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
随机推荐
- Spring常用的接口和类(一)
一.ApplicationContextAware接口 当一个类需要获取ApplicationContext实例时,可以让该类实现ApplicationContextAware接口.代码展示如下: p ...
- CameraFacingBillboard
原地址:http://www.cnblogs.com/88999660/ 描述 这个脚本使得它被连接到配合本身相机的对象.这对于要经常面对镜头,并以同样的方式了,因为它是有用的广告牌. 用法 将这个脚 ...
- [BZOJ1659][Usaco2006 Mar]Lights Out 关灯
[BZOJ1659][Usaco2006 Mar]Lights Out 关灯 试题描述 奶牛们喜欢在黑暗中睡觉.每天晚上,他们的牲口棚有L(3<=L<=50)盏灯,他们想让亮着的灯尽可能的 ...
- 如何高效利用GitHub
是Github,让社会化编程成为现实.本文尝试谈谈GitHub的文化.技巧与影响. Q1:GitHub是什么 Q2:GitHub风格 Q3: 在GitHub,如何跟牛人学习 Q4: 享受纯粹的写作与演 ...
- 【云计算】Kubernetes、Marathon等框架需要解决什么样的问题?
闲谈Kubernetes 的主要特性和经验分享 Capitalonline全球云主机.全球私有网络,免费试用进行时 » 主要介绍 Kubernetes 的主要特性和一些经验.先从整体上 ...
- Sharepoint程序员应该了解的知识
做为一个Sharepoint程序员应该了解的知识:注意,我说的是程序员.因为我一直把自己看一个普普通通的程序员. 前提: 要知道网络基础(包括DHCP.IP.掩码.DNS.网关.广播),会装操作系统( ...
- Avisynth + DirectShow + WebCamera 实现Avisynth脚本访问摄像头
准备工作:需要以下三种软件 1.Avisynth_258 安装文件和源码下载地址(Avisynth_258.exe 4.2 MB) http://sourceforge.net/projects/av ...
- 基于centos搭建nginx+uwsgi运行django环境
环境: CentOS 7 nginx/1.9.12 Python 2.7.5 一:安装依赖包5 yum install zlib-devel bzip2-devel pcre-devel openss ...
- codeforces B. Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...
- codeforces A. Cinema Line 解题报告
题目链接:http://codeforces.com/problemset/problem/349/A 题目意思:题目不难理解,从一开始什么钱都没有的情况下,要向每一个人售票,每张票价格是25卢布,这 ...