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.

按照这个思路, 对于从右数第iA[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个开始的.

至此, 结合上一题, 已经能看出规律: 对于从右数第iA[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) (至少. facgenerateNum的复杂度取决于输入.)

空间复杂度: O(n)

[OJ] Permutation Index的更多相关文章

  1. Permutation Index I & II

    Given a permutation which contains no repeated number, find its index in all the permutations of the ...

  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. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  7. 《R包的分类介绍》

    R分析空间数据(Spatial Data) R机器学习包(Machine Learning) R多元统计包(Multivariate Statistics) R药物(代谢)动力学数据分析包 R计算计量 ...

  8. R语言︱常用统计方法包+机器学习包(名称、简介)

    一.一些函数包大汇总 转载于:http://www.dataguru.cn/thread-116761-1-1.html 时间上有点过期,下面的资料供大家参考基本的R包已经实现了传统多元统计的很多功能 ...

  9. leetcode 22括号生成

    非常好的一道题.一开始的思想是这样的,先把n对括号按照某一顺序生成一个string,然后用全排列算法生成所有可能,然后利用stack写一段判断括号是否匹配的字符串,匹配的假如结果中.不过会超时.因为全 ...

随机推荐

  1. xml中使用foreach遍历对象

    如果是一个带数据的List对象 <select id="selectProductMSTList" resultType="java.util.Map" ...

  2. readonly和const区别

    常量和只读变量的区别 const string name="Xuj"; readonly string name; 1.常量是不可改变的,只读变量只能在构造方法中才能改变其值. 2 ...

  3. jQuery $.each的使用方法

    通过jQuery $.each,你可以遍历对象.数组的属性值并进行处理. 使用说明 each函数根据参数的类型实现的效果不完全一致: 1.遍历对象(有附加参数) $.each(Object, func ...

  4. SVN的那些事

    1,终端报错:is too old (format 29) to work with client version '1.9.4 (r1740329)' (expects format 31). Yo ...

  5. 关于使用navigationController,前后2个视图控制器navigationBar隐藏属性不同,导致右滑手势失效问题的解决办法

    ###1.问题描述:如A是navigationController的rootViewController,在这个页面navigationBar是显示的(隐藏属性为NO),它push圧栈过来B视图控制器 ...

  6. asp.net:repeater嵌套(常用于新闻等在首页归类显示)

    using System;using System.Configuration;using System.Collections.Generic;using System.Linq;using Sys ...

  7. javascript dom 编程艺术笔记 第四章:图片库

    首先先建立一个html文件,建立一个图片库 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

  8. bzoj2732: [HNOI2012]射箭 半平面交

    这题乍一看与半平面交并没有什么卵联系,然而每个靶子都可以转化为两个半平面. scanf("%lf%lf%lf",&x,&ymin,&ymax); 于是乎就有 ...

  9. jQuery学习 day01

    最近受某大牛指点(我不会说他姓范),了解了一下jQuery,据说很牛X,就了解了一下,第一天,分享给大家一些心得吧. 1.首先就是导入jQuery文件了,这里我是去jQuery官网下载的.(大家可以去 ...

  10. 对C#泛型中的new()约束思考

    对于new()约束,大家可能有一个误解,以为使用了new约束之后,在创建对象时与非泛型的版本是一致的:<public class Tester<T> where T:new() {  ...