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.

分析:http://www.cnblogs.com/EdwardLiu/p/5104310.html

以4,1,2为例,4为第3大数,1为剩余序列第1大数,2为剩余序列第1大数,

故表达式为:2*2! + 0*1! + 0*0! + 1 = 5

以2,4,1为例,2为第2大数,4为剩余序列第2大数,1为剩余序列第1大数

故表达式为:1*2! + 1*1! + 0*0! + 1 = 4

这后面这个1一定要加,因为前面算的都是比该数小的数,加上这个1,才是该数是第几大数。

对于2*2!,2!表示当时当前位后面还有两位,全排列有2!种, 第一个2表示比4小的有两个数。全排列可以以它们开头。

 public class Solution {
public long permutationIndex(int[] A) {
long index = , fact = ;
for (int i = A.length - ; i >= ; i--) {
int numOfSmaller = ;
for (int j = i + ; j < A.length; j++) {
if (A[j] < A[i]) numOfSmaller++; // numOfSmaller refers to the numbers which can begin with;
}
index += numOfSmaller * fact;
fact *= (A.length - i);
}
return index + ;
}
}

Permutation Index II

 Given a permutation which may contain repeated numbers, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.

Example

Given the permutation [1, 4, 2, 2], return 3.

分析:https://segmentfault.com/a/1190000004683277

与上一题的不同之处时会有重复的数。那么,只要在发现是重复数的那一位用numOfSmallers* fact的结果除以重复的次数dup再加入index就可以了。当然,每个重复数的dup都要阶乘,例如有3个2,4个8,dup就是3! * 4! = 144index是所有previous排列的次数和,返回下一次index+1

 import java.util.HashMap;

 public class Solution {
public long permutationIndexII(int[] A) {
long index = , fact = , dup = ;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = A.length - ; i >= ; i--) {
if (!map.containsKey(A[i])) {
map.put(A[i], );
} else {
map.put(A[i], map.get(A[i]) + );
dup *= map.get(A[i]);
}
int numOfSmallers = ;
for (int j = i + ; j < A.length; j++) {
if (A[j] < A[i])
numOfSmallers++;
}
index += numOfSmallers * fact / dup;
fact *= (A.length - i);
}
return index + ;
}
}

Permutation Index I & II的更多相关文章

  1. [OJ] Permutation Index

    LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...

  2. Lintcode: Permutation Index II

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

  3. lintcode :Permutation Index 排列序号

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

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

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

  5. lintcode Permutation Index

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

  6. Codeforces Round #598 (Div. 3) B Minimize the Permutation

    B. Minimize the Permutation You are given a permutation of length nn. Recall that the permutation is ...

  7. [LeetCode] Find Permutation 找全排列

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  8. leetcode -- permutation 总结

    leetcode上关于permutation有如下几题 Permutation Sequence Next Permutation Permutations Permutations II

  9. [LintCode]——目录

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

随机推荐

  1. Django 2.0 学习(21):Django Session

    Django Session Session 与Cookie 1.简介 1.Cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们又需要"保持状态",因 ...

  2. CF992C Nastya and a Wardrobe

    我是题面 题意很清晰,这种题,我们当然还是有两种方法来做啦 方法一:找规律 读完题我们来看样例,通过样例一已我们大概可以看出,答案或许是\(n*2^{k+1}\) 肯定不能这么简单对吧,那就来看样例二 ...

  3. MT【141】逆用特征根法

    (清华大学THUSSAT) 已知 \(a=\left( \dfrac{-1+\sqrt{5}}{2} \right)^{-10}+\left( \dfrac{-1-\sqrt{5}}{2} \righ ...

  4. Android指纹识别API讲解,让你有更好的用户体验

    我发现了一个比较怪的现象.在iPhone上使用十分普遍的指纹认证功能,在Android手机上却鲜有APP使用,我简单观察了一下,发现Android手机上基本上只有支付宝.微信和极少APP支持指纹认证功 ...

  5. 一种KEIL中定义过的变量在使用中提示未定义的情况

    [环境] > KEIL5.25 > win10 > @2018-4-23 [问题] 头文件互包含导致的错误(使用了另一文件的类型定义) 文件<fileA.h> <f ...

  6. 洛谷 P3205 [HNOI2010]合唱队 解题报告

    P3205 [HNOI2010]合唱队 题目描述 为了在即将到来的晚会上有更好的演出效果,作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形.假定合唱队一共N个人,第i个人的身高为 ...

  7. 解题:NOI 2014 动物园

    题面 其实好像并不难,因为猫老师(应该是猫老师吧,还是LX大佬?)有一句话让我印象深刻:“包的(border)的包的还是包的”=.= 统计个数不就是统计长度么,然后根据上面那句话,当$nxt$长度大于 ...

  8. 【bzoj4084】【sdoi2015】双旋转字符串

    题解 首先题中说了$n>=m$; 分成的循环串左右两边为本质相同的单循环串循环串,分别长为$l = \frac{n + m}{2} $; 所以$S$串的前$l$位为双循环串的一半$S1$,后一半 ...

  9. oracle的loop

    oracle的loop等循环语句的几个用法小例子 --loop循环用法 (输出1到10) ) :; begin loop v_num :; ; --上面退出循环也可以用下面3行 /* if(v_num ...

  10. 安装JDK、Tomcat、Maven’详细步骤

    安装JDK 1.首先在官网下载JDK1.8包并解压(随便你装哪个版本) 2.下面开始配置环境变量  此电脑-右键-属性-高级系统配置  点击环境变量 将会看到以下界面 在系统变量下“新建” 变量名(J ...