Permutation Index I & II
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.
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 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! = 144
。index
是所有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的更多相关文章
- [OJ] Permutation Index
LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...
- Lintcode: Permutation Index II
Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...
- 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 Permutation Index
题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...
- 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 ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- leetcode -- permutation 总结
leetcode上关于permutation有如下几题 Permutation Sequence Next Permutation Permutations Permutations II
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
随机推荐
- SD/MMC相关寄存器的介绍
1.SD卡内部架构 在熟悉SD/MMC相关寄存器之前,我们先来看看SD卡的内部架构是怎么样的,如下图所示: 2.SD/MMC相关寄存器的介绍 从上图中总结出:SD卡内部有7个寄存器. 一.OCR,CI ...
- BZOJ5294 BJOI2018二进制(线段树)
二进制数能被3整除相当于奇数.偶数位上1的个数模3同余.那么如果有偶数个1,一定存在重排方案使其合法:否则则要求至少有两个0且至少有3个1,这样可以给奇数位单独安排3个1. 考虑线段树维护区间内的一堆 ...
- 【bzoj2594】 Wc2006—水管局长数据加强版
http://www.lydsy.com/JudgeOnline/problem.php?id=2594 (题目链接) 题意 给出一个带边权的无向简单,要求维护两个操作,删除${u,v}$之间的连边: ...
- python与pycharm
什么叫自动化测试? 通俗来说,自动化测试就是通过写代码来帮我们测试软件.用来做自动化测试的语言很多,python,Java,php,Go,ruby等.而且软件系统开发语言与自动化测试语言可以不一致.例 ...
- windows下64位python的安装及机器学习相关包的安装(实用)
开通博客已久,想了好久决定写个基础的安装教程,望后人少走弯路,也借此希望跟大家多多交流.文中给出的链接默认是基于对python2.7的前提下的包. 1.首先下载64位Python包,进行安装(默认py ...
- openstack中的server
一.HTTP server 主要是horizon模块,horizon是基于Python Django搭建的web应用,其运行于Apache网络服务器上(当然也可以运行在其他web服务器上),主要功能就 ...
- UITableViewCell在非Nib及Cell重用下设置CellStyle
在UITableViewController(实现了UITableViewDataSource)下需要实现 - (UITableViewCell *)tableView:(UITableView *) ...
- 科学计算三维可视化---Mlab基础(管线控制函数)
科学计算三维可视化---TVTK管线与数据加载(可视化管线和图像管线了解) 科学计算三维可视化---Mayavi入门(Mayavi管线) Mlab管线控制函数的调用 Sources:数据源 Filte ...
- Java RMI 简单实现
提供远端访问的时候,我们首先需要定义远端能够访问哪些东西,在Java中,定义这类接口需要实现Remote接口 public interface Business extends Remote{ pub ...
- MyEclipse和Eclipse中jsp、html格式化自动排版问题
一.myeclipse的漂亮排版设置 步骤: 在左侧快捷 “搜索” 框里面输入 html . 点击选中左侧HTML Source . line - width 是设置当前行里面有多少字符时,就换行.这 ...