Lintcode: 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. 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的更多相关文章
- lintcode Permutation Index
题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...
- Permutation Index I & II
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
- [OJ] Permutation Index
LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...
- 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 ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- [LintCode] Permuation Index
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
- [LeetCode] Palindrome Permutation I & II
Palindrome Permutation Given a string, determine if a permutation of the string could form a palindr ...
随机推荐
- ActionResult,PartialViewResult,EmptyResult,ContentResult
HttpGet HttpPost HttpDelete HttpPut HeepHead HttpOptions HttpPatch属性都是动作方法选定器的一份子,比如若在action上套用H ...
- Cacti安装详细步骤
原文链接: https://www.centos.bz/2012/01/cacti-install-tutorials/ Cacti-监控MySQL: http://www.cszhi.com/201 ...
- Java - HttpURLConnection
JDK中的URLConnection参数详解 1:> URL请求的类别: 分为二类,GET与POST请求.二者的区别在于: a:) get请求可以获取静态页面,也可以把参数放在URL字串后面,传 ...
- Delphi 指针
1:指针的赋值. type RTestInfo = record Age:Integer; end; PtestInfo = ^ RtestInfo; var Test1,Test2:PtestInf ...
- java编程算法
一.字符串相关操作 String s = " Hello java,hello android,hello OOP,HELLO String,hello JAVASE!"; Sys ...
- 【转】如何使php的MD5与C#的MD5一致?
有c#生成MD5的代码如下: class CreateMD5 { static void Main(string[] args) { string source = "提问指南"; ...
- settimeout 传递带有参数的函数
方法一:传递带有参数的function给settimeout,写个函数,该函数返回一个不带参数的函数 <script language="javascript"> fu ...
- css中textarea去掉边框和选中后的蓝色边框问题的解决方法
我们在设计网页的输入框时,有时会遇到需要把textarea的边框去掉的问题,经过测试,下面的代码是可以的. textarea{ border: solid 0px; outline:none; }
- php--城市分类
效果图:
- 安装sqlserver2008r2 服务器配置,服务帐户配置出错,提示Sql server服务指定的凭据无效
win+X 点击运行 重置帐户密码使得sql server2008 的服务帐户名,密码与系统设置的Administrator名与密码一致,则可. 上图中有一项reporting的名字不同,该名字为自动 ...