Lintcode: Permutation Index

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.

在计算最终的 index 时需要动态计算某个数的相对大小。我们可通过两重循环得出到某个索引处值的相对大小。

正确

以4,1,2为例,4为第3大数,1为剩余序列第1大数,2为剩余序列第1大数,

故表达式为:(3-1)*2! + (1-1)*1! + (1-1)*0! + 1 = 5

以2,4,1为例,2为第2大数,4为剩余序列第2大数,1为剩余序列第1大数

故表达式为:(2-1)*2! + (2-1)*1! + (1-1)*0! + 1 = 4

这后面这个1一定要加,因为前面算的都是比该数小的数,加上这个1,才是该数是第几大数。

2!表示当时当前位后面还有两位,全排列有2!种

 1 public class Solution {
 2     /**
 3      * @param A an integer array
 4      * @return a long integer
 5      */
 6     public long permutationIndex(int[] A) {
 7         // Write your code here
 8         long res = 0;
 9         int n = A.length;
10         long fact = 1;
11         for (int i=1; i<n; i++) {
12             fact *= i; //fact should at last equal (n-1)!
13         }
14         int initial = n-1; //use to update factorial
15         for (int i=0; i<n; i++) {
16             long count = 0;
17             for (int j=i; j<n; j++) {
18                 if (A[i] >= A[j]) {
19                     count++;
20                 }
21             }
22             res += (count-1)*fact;
23             if (initial != 0) {
24                 fact /= initial;
25                 initial--;
26             }
27         }
28         res = res + 1;
29         return res;
30     }
31 }
时间: 2024-08-02 16:41:27

Lintcode: Permutation Index的相关文章

LintCode &quot;Permutation Index II&quot; !

Simply a variation to "Permutation Index". When calculating current digit index, we consider duplicated case. Again, similar as "Digit Counts", it is another counting problem and stil digit by digit. And please note: we can use Fenwick

Permutation Index I &amp; 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. Example Given [1,2,4], return 1. 分析:http://www.cnblogs.com/EdwardLiu/p/

lintcode 容易题:Permutation Index 排列序号

题目: 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号.其中,编号从1开始. 样例 例如,排列[1,2,4]是第1个排列. 解题: 这个题目感觉很坑的.感觉这只有求出所有的排列,然后找出其对应的下标,但是怎么求出排列,在做Project Euler 时候碰到过,但是现在我又不会写了,那时候毕竟是抄别人的程序的.在geekviewpoint看到一种很厉害的解法,不需要求所有的排列,直接根据给的数组进行求解. 思路: 1.对于四位数:4213 = 4*100+2

[LintCode] Permuation Index

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. class Solution { public: /** * @param

利用编程解决一道数学题

问题 在朋友QQ空间看到一道题,如下: 当时顺手画了条数轴,在数轴上标出了各个算式的解的特点:和为7的算式的解关于4对称,和为9的解关于5对称等等.草草算了下,发现很难同时满足5个条件.而细算又太麻烦,算了,反正是程序员,写程序求解吧. 利用笛卡尔积求解 因为是4个算式,很自然的就想到穷举法.将每个算式可能的结果放在一起算笛卡尔积,如果有解的话,则必然存在一个笛卡尔积里面存在1到8这8个不同的元素. 计算笛卡尔积的代码如下: /// <summary> /// 计算元素集列表的笛卡尔积 ///

R语言 机器学习包

from:http://www.zhizhihu.com/html/y2009/410.html 机器学习是计算机科学和统计学的边缘交叉领域,R关于机器学习的包主要包括以下几个方面:  1)神经网络(Neural Networks): nnet包执行单隐层前馈神经网络,nnet是VR包的一部分(http://cran.r-project.org/web/packages/VR/index.html).  2)递归拆分(Recursive Partitioning): 递归拆分利用树形结构模型,来

ML—机器学习常用包(持续更新….)

机器学习是计算机科学和统计学的边缘交叉领域,R关于机器学习的包主要包括以下几个方面: 1)神经网络(Neural Networks): nnet.AMORE以及neuralnet,nnet提供了最常见的前馈反向传播神经网络算法.AMORE包则更进一步提供了更为丰富的控制参数,并可以增加多个隐藏层.neuralnet包的改进在于提供了弹性反向传播算法和更多的激活函数形式.但以上各包均围绕着BP网络,并未涉及到神经网络中的其它拓扑结构和网络模型.而新出炉的RSNNS包则在这方面有了极大的扩充 2)递

n阶行列式的全排列求解(Java)

上一个随笔,我介绍了全排列的递归求解,其中还有排列的逆序数等代码,这次我来介绍如何使用全排列计算行列式的值. 使用全排列求行列式的值,简单的描述就是: 对这个行列式每一行选取一个数,这些数处于行列式的不同的列,将这些数相乘,结果记为A_1 将这些数的列标按行标从上到下的顺序排列,如果这个排列的逆序数为偶数,A_1加个正号+A_1,否则加个负号-A_1 由排列组合知识我们知道我们一共能从行列式中取出n!种情况.他们的和就是行列式的值 (刚开始用博客园,没找到插入latex的地方,我就截个图了...

[Lintcode]52. Next Permutation

52. Next Permutation 本题难度: Medium Topic: Greedy Description 52. Next Permutation 本题难度: Medium Topic: Greedy Description Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Example Example 1: Input:[1] O