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. zabbix自定义web检测

    zabbix自定义web检测 本博客使用zabbix 版本 4.0.3 Web监控的原理 Web监控即对HTTP服务的监控,模拟用户去访问网站,对特定的结果进行比较,如状态码.返回字符串等特定的数据进 ...

  2. nginx实现ldap认证

    1.安装依赖. yum -y install openldap-devel yum install pcre pcre-devel -y yum -y install openssl openssl- ...

  3. Timing wheel心跳机制

    在web服务中,断开空闲连接是一种减少资源浪费的一种手段,由此就有了心跳机制来判断一个连接是否空闲. 一种简单粗暴的方式: 1. 服务端每个连接保存一个最后一次操作的时间戳,每次这个连接对应fd可读时 ...

  4. BZOJ1495 [NOI2006]网络收费 【树形dp + 状压dp】

    题目链接 BZOJ1495 题解 观察表格,实际上就是分\(A\)多和\(B\)两种情况,分别对应每个点选\(A\)权值或者\(B\)权值,所以成对的权值可以分到每个点上 所以每个非叶节点实际对应一个 ...

  5. 【bzoj3751】 Hnoi2014—画框

    http://www.lydsy.com/JudgeOnline/problem.php?id=3571 (题目链接) 题意 给出一个$2*N$个点的二分图,$N*N$条边,连接$i$和$j$的边有两 ...

  6. python3.6关键字总结

    模块是个好东西 import keyword # 导入关键字模块 lst = keyword.kwlist # 实例化 print(lst) # 看看有哪些玩意 print(len(lst)) # 貌 ...

  7. 使用spring cache和ehcache

    一.spring cache Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存放在缓存中,等到下次利用同样的参数来调用该 ...

  8. Memcache PHP 使用笔记

    Memcache PHP 使用笔记 最近在做网站迁移 看到之前的一个网站目录下Cache文件里上万的缓存文件真是害怕 新的服务器上配置了memcache扩展 于是乎准备折腾一下看看能不能把之前的文件缓 ...

  9. python3 安装MySQLdb及无法打开mysql.h问题解决(win7 )

    在Flask中要连接mysql数据库,需要安装flask-mysqldb. pip install flask-mysqldb 用pip安装MySQLdb 中遇到如下问题,折腾半天之久,多方尝试,终搞 ...

  10. 「Vue」v-on修饰符

    修饰符stop阻止冒泡 --> <!-- <div id="myvue" @click="divc" class="d1" ...