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. Linux命令echo -e

    在Linux命令中 echo -e 这个参数e是什么意思. echo –e “I will use ‘touch’ command to create 3 files.” 这里参数e的作用是什么 ma ...

  2. <?php set_time_limit(10);

    <?php include('w_fun.php'); set_time_limit(10); Fatal error: Maximum execution time of 10 seconds ...

  3. 下面我会介绍几种轻轻松松访问Google的方法

    好人一生平安的大招 Google在大陆已经封了差不多有20天   访问是极其的困难 下面我会介绍几种轻轻松松访问Google的方法 首先 你需要个可靠的hosts  比如 https://git.os ...

  4. js弹出确认框,挺全

    一种: <a href="javascript:if(confirm('确实要删除该内容吗?'))location='http://www.google.com'">弹 ...

  5. php---初学者git使用

    1.git自学网站 廖雪峰的官方网站 2.安装一个简单的git 创建用户名.邮箱 git config --global user.name "your name" git con ...

  6. HTML5 拖拽功能

    本地文件拖动到页面实例:(支持IE) <script> var DragFile = function (goalId) { var g = document.getElementById ...

  7. 用facebook账号登陆到你的Magento网店

    Inchoo提供magento和facebook连接的扩展,可以到http://inchoo.net/ecommerce/magento/facebook-connect-magento-extens ...

  8. [LeetCode]题解(python):107 Binary Tree Level Order Traversal II

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...

  9. Mac版 MicrosoftOffice2015 办公软件 破解教程

    来自:http://bbs.feng.com/read-htm-tid-9704285.html 下载链接:http://pan.baidu.com/s/1dD6lBFz  提取密码:xu5n 补丁: ...

  10. SMART Goals

    Once you have planned your project, turn your attention to developing several goals that will enable ...