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. ansible操作(一)

    ansible晋级操作之ad-hoc命令 所谓的ad-hoc命令! 如果我们敲入一些命令去比较快的完成一些事情,而不需要将这些执行的命令特别保存下来, 这样的命令就叫做 ad-hoc 命令.Ansib ...

  2. 函数防抖与函数节流 封装好的debounce和throttle函数

    /** * 空闲控制 返回函数连续调用时,空闲时间必须大于或等于 wait,func 才会执行 * * @param {function} func 传入函数,最后一个参数是额外增加的this对象,. ...

  3. 转:机器学习中的算法(2)-支持向量机(SVM)基础

    机器学习中的算法(2)-支持向量机(SVM)基础 转:http://www.cnblogs.com/LeftNotEasy/archive/2011/05/02/basic-of-svm.html 版 ...

  4. [SDOI2013]项链

    description luogu 最近,铭铭迷恋上了一种项链.与其他珍珠项链基本上相同,不过这种项链的珠子却与众不同,是正三菱柱的泰山石雕刻而成的. 三菱柱的侧面是正方形构成的,上面刻有数字. 能够 ...

  5. 20135239益西拉姆 Linux内核分析 进程的描述和进程的创建

    [益西拉姆 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] 第六周 进程的描述 ...

  6. phpredis pipeline

    通过pipeline方式将client端命令一起发出,redis server会处理完多条命令后,将结果一起打包返回client,从而节省大量的网络延迟开销.

  7. 牛客网NOIP赛前集训营-普及组(第二场)

    T1 牛牛刚学习了输入输出,他遇到了一道这样的题目. 输入2个整数a和b 保证输入的a和b在long long范围之内,即满足 -9223372036854775808 <= a, b < ...

  8. 1044 Shopping in Mars

    Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diam ...

  9. a标签--超链接

    一.链接到其他网站 <body> <a href="https://www.baidu.com" target="_blank">百度& ...

  10. python的reduce函数的使用方法详解以及使用案例,相加,相乘(处理一个序列,然后把序列进程合并操作)

    1.求列表的数字相加之和,还是之前的习惯,写for循环来实现 num_1=[1,2,3,4,5,6,7,8,9] a=0 for n in num_1: #a=a+n a+=n print (a) C ...