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.

Have you met this question in a real interview? Yes
Example
Given the permutation [1, 4, 2, 2], return 3.

这里需要考虑重复元素,有无重复元素最大的区别在于原来的1!, 2!, 3!...等需要除以重复元素个数的阶乘。记录重复元素个数同样需要动态更新,引入哈希表这个万能的工具较为方便。

按照从数字低位到高位进行计算

 public class Solution {
/**
* @param A an integer array
* @return a long integer
*/
public long permutationIndexII(int[] A) {
// Write your code here
if (A==null || A.length==0) return new Long(0);
int len = A.length;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
long index = 0, fact = 1, mulFact = 1;
for (int i=len-1; i>=0; i--) {
if (!map.containsKey(A[i])) {
map.put(A[i], 1);
}
else {
map.put(A[i], map.get(A[i])+1);
mulFact *= map.get(A[i]);
}
int count = 0;
for (int j=i+1; j<len; j++) {
if (A[i] > A[j]) count++;
}
index += count*fact/mulFact;
fact *= (len-i);
}
index = index + 1;
return index;
}
}

Lintcode: Permutation Index II的更多相关文章

  1. lintcode Permutation Index

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

  2. Permutation Index I & II

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

  3. [OJ] Permutation Index

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

  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. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  7. [LintCode] Permuation Index

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

  8. [Locked] Palindrome Permutation I & II

    Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...

  9. [LeetCode] Palindrome Permutation I & II

    Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...

随机推荐

  1. MySQL 5.6.3

    SHOW VARIABLES LIKE '%version%'; https://dev.mysql.com/doc/refman/5.6/en/explain.html As of MySQL 5. ...

  2. JAVA Exchanger

    //Exchanger工具类的使用案例 //本文给出一个简单的例子,实现两个线程之间交换数据,用Exchanger来做非常简单. import java.util.concurrent.Exchang ...

  3. nodejs爬虫

    前言 几个月之前,有同事找我要PHP CI框架写的OA系统.他跟我说,他需要学习PHP CI框架,我建议他学习大牛写的国产优秀框架QeePHP. 我上QeePHP官网,发现官方网站打不开了,GOOGL ...

  4. 【Java IO】FileInputStream 和 FileOutputStream

    class FileInputStream extends  InputStream implements Closeable

  5. Window上装PHP开发环境 (XAMPP)

    原作者:http://www.cnblogs.com/martin1009/archive/2011/11/11/2245794.html 1. 从www.apachefriends.org 上下载X ...

  6. 为WordPress某个文章添加额外的样式

    如需把css直接写在某文章,把下面代码放如function.php /* 为特定文章添加特定css最简单的方式. */ /*添加自定义CSS的meta box*/ add_action('admin_ ...

  7. PMP--可能会涉及到的计算题

    一.进度管理里的历时三点估算历时的三点估算可能会出现在进度管理的计算题里.以下公式,大家要记住:说一下历时的三点估算中的几个值:1.最有可能的历时估算:Tm2.最乐观的历时估算: To3.最悲观的历时 ...

  8. php mysql连接例子

    <?PHP @$conn = mysql_connect("127.0.0.1","root",""); //返回false或reso ...

  9. attr属性

    1.切换图片 $(".tip").hover(        function () {            $(this).attr("src"," ...

  10. ps、grep和kill联合使用杀掉进程(转)

    例如要杀掉hello这个进程,使用下面这个命令就能直接实现.   ps -ef |grep hello |awk '{print $2}'|xargs kill -9 这里是输出ps -ef |gre ...