[OJ] Permutation Index
LintCode 197. Permutation Index (Easy)
LintCode 198. Permutation Index II (Medium)
感觉这两道题主要考察计算排列组合的能力.
Permutation Index
举例:
123 -> 1
132 -> 2
213 -> 3
231 -> 4
312 -> 5
321 -> 6
以321为例进行分析:
首先考虑第一位3: 3右边比3小的数字有两个(1和2), 所以以1和2为首位的数字排在3xx的前面, 这样的数字有2 * 2! = 4个. 所以以3开头的数字至少排第5.
3已经考虑完, 看后面两位.
12 -> 1
21 -> 2
在321中, 2右边比2小的数字有一个(1), 所以以1为首位的数字排在2x的前面, 这样的数字有1 * 1! = 1个.
最后一个1, 只有一个数字, 排在它前面的数字是0个.
综上, 321前面排了5个数字, 所以它的Permutation Index是6.
按照这个思路, 对于从右数第i位A[i](i = 0, 1, 2...), 若它的右边有k个数字小于A[i], 那么这一位就会在Permutation Index中贡献k * i!.
class Solution {
public:
/**
* @param A an integer array
* @return a long integer
*/
long long permutationIndex(vector<int>& A) {
int N = A.size();
long long index = 1;
long long mul = 1;
for (int i = N - 2; i >= 0; --i) {
int cnt = 0;
for (int j = i + 1; j < N; ++j) {
if (A[j] < A[i]) ++cnt;
}
index += cnt * mul;
mul *= N - i;
}
return index;
}
};
时间复杂度: O(n^2)
空间复杂度: O(1)
Permutation Index II
做完了Permutation Index我看了下九章的解答. 我去, 怎么这么复杂. 看了一半看不下去了, 然后发现九章上的Permutation Index II用的一样的解法, 说明九章只是把第二题的解法直接复制到第一题里了.
自己想这题花了好久好久, 顿感高中数学忘得差不多了(T_T).
举例:
11223 -> 1
11232 -> 2
11322 -> 3
12123 -> 4
12132 -> 5
12213 -> 6
12231 -> 7
12312 -> 8
12321 -> 9
13122 -> 10
考虑13122:
第一位1, 没有比1再小的数字了, 所以1开头的数字是从第一个开始的.
第二位3, 3右边比3小的数字有1和2.
- 如果1和3互换位置, 后面三位将是数字2,2,3. 这三个数字的组合数是
3! / 2! = 3个. - 如果2和3互换位置, 后面三位将是数字1,2,3. 这三个数字的组合数是
3! = 6个.
所以因为第二位比3小(首位是1)而排在13xxx前面的数字有9个. 因此13xxx一定是从第10个开始的.
至此, 结合上一题, 已经能看出规律: 对于从右数第i位A[i], 看它右边的每一个比A[i]小的数字, 假设A[j] < A[i] (j > i), 那么假想A[j]和A[i]互换位置后, 计算右边的i-1个数字的组合数就是这A[j]贡献的. 要注意的是, A[i]右边可能有多个比A[i]小的重复数字, 这些数字只贡献一次.
class Solution {
private:
map<int, int> m;
long long fac(int num) {
long long n = 1;
while (num > 0) {
n *= num;
num--;
}
return n;
}
long generateNum() {
long long num = 1;
for (auto it = m.begin(); it != m.end(); ++it) {
num *= fac(it->second);
}
return num;
}
public:
/**
* @param A an integer array
* @return a long integer
*/
long long permutationIndexII(vector<int>& A) {
int N = A.size();
if (N == 0) return 0;
m.clear();
long long index = 1;
for (int num : A) {
if (m.find(num) != m.end()) {
++m[num];
} else {
m[num] = 1;
}
}
for (int i = 0; i < N; ++i) {
set<int> s;
for (int j = i + 1; j < N; ++j) {
if (A[j] < A[i] && s.find(A[j]) == s.end()) {
m[A[j]]--;
index += fac(N - i - 1) / generateNum();
s.insert(A[j]);
m[A[j]]++;
}
}
m[A[i]]--;
if (m[A[i]] == 0) m.erase(A[i]);
}
return index;
}
};
时间复杂度: O(n^2) (至少. fac和generateNum的复杂度取决于输入.)
空间复杂度: O(n)
[OJ] Permutation Index的更多相关文章
- Permutation Index I & II
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
- 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/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 《R包的分类介绍》
R分析空间数据(Spatial Data) R机器学习包(Machine Learning) R多元统计包(Multivariate Statistics) R药物(代谢)动力学数据分析包 R计算计量 ...
- R语言︱常用统计方法包+机器学习包(名称、简介)
一.一些函数包大汇总 转载于:http://www.dataguru.cn/thread-116761-1-1.html 时间上有点过期,下面的资料供大家参考基本的R包已经实现了传统多元统计的很多功能 ...
- leetcode 22括号生成
非常好的一道题.一开始的思想是这样的,先把n对括号按照某一顺序生成一个string,然后用全排列算法生成所有可能,然后利用stack写一段判断括号是否匹配的字符串,匹配的假如结果中.不过会超时.因为全 ...
随机推荐
- Oracel 数据库函数
-- Oracle 函数 学习 -- 数值函数 ,(四舍五入, 取整,常用计算,三角) -- 1.四舍五入 round(n[,m]) ,省略m :表示 0 ;m>0 ;小数点后m位 ;m< ...
- JS中的replace方法以及与正则表达式的结合应用
replace方法的语法是:stringobj.replace(rgexp, replacetext) 其中stringobj是字符串(string),reexp可以是正则表达式对象(regexp)也 ...
- Oracle的%type和%rowtype
1 %TYPE说明 为了使一个变量的数据类型与另一个已经定义了的变量(尤其是表的某一列)的数据类型相一致,Oracle提供 了%TYPE定义方式.当被参照的那个变量的数据类型改变了之后,这个新定义的变 ...
- ORACLE 数据库用户备份及表备份
表备份模式备份:exp system/pwd@127.0.0.1:1521/db owner=(user) file=E:\DB\db20150326.dmp tables=(table);还原 ...
- Net中exe之间的消息传递
1.创建一个消息通讯类 using System;using System.Collections.Generic;using System.Linq;using System.Text;using ...
- LA 3177 Beijing Guards(二分法 贪心)
Beijing Guards Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the ...
- C++数组(指针)作为函数参数
本文的学习内容参考:http://blog.csdn.net/wwdlk/article/details/6322843 1.当用数组名作为函数参数时,函数的实参和形参都应为数组名(或者指针): Ex ...
- 九度OJ 1371 最小的K个数 -- 堆排序
题目地址:http://ac.jobdu.com/problem.php?pid=1371 题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4 ...
- 【转】oracle Sequence
http://blog.csdn.net/zhoufoxcn/article/details/1762351 在oracle中sequence就是序号,每次取的时候它会自动增加.sequence与表没 ...
- grunt-mac上安装运行构建工具的总结(一)
安装node.js brew install node.js 安装grunt npm install -g grunt-cli 1.新建package.json,配置 { "name&quo ...