lintcode :Permutation Index 排列序号
题目:
给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的编号。其中,编号从1开始。
例如,排列[1,2,4]是第1个排列。
解题:
这个题目感觉很坑的。感觉这只有求出所有的排列,然后找出其对应的下标,但是怎么求出排列,在做Project Euler 时候碰到过,但是现在我又不会写了,那时候毕竟是抄别人的程序的。在geekviewpoint看到一种很厉害的解法,不需要求所有的排列,直接根据给的数组进行求解。
思路:
1.对于四位数:4213 = 4*100+2*100+1*10+3
2.4个数的排列有4!种。当我们知道第一位数的时候,还有3!种方式,当知道第二位数时候还有2!种方式,当知道第三位数的时候还有1!种方式,前面三位数都确定的时候,最后一位也确定了。<这里是按照高位到地位的顺序>
3.对4个数的排列,各位的权值为:3!,2!,1!,0!。第一位之后的数小于第一位的个数是x,第二位之后的数小于第二位的个数是y,第三位之后的数小于第三的个数是z,第四位之后的数小于第四位的个数是w,则abcd排列所在的序列号:index = x*3!+y*2!+z*1!,<0!=0>
在数的排列中,小数在前面,大数在后面,所以考虑该位数之后的数小于该为的数的个数,这里我自己理解的也不是很透,就这样。
4.例如 4213;x= 3,y = 1,z=0,index = 18+2=20
123;x = 0,y=0,index = 0
321;x= 2,y=1,index = 2*2!+1*1! = 5
这里的下标是从0开始的。
Java程序:
public class Solution {
/**
* @param A an integer array
* @return a long integer
*/
public long permutationIndex(int[] permutation) {
// Write your code here
long index = 0;
long position = 2;// position 1 is paired with factor 0 and so is skipped
long factor = 1;
for (int p = permutation.length - 2; p >= 0; p--) {
long successors = 0;
for (int q = p + 1; q < permutation.length; q++) {
if (permutation[p] > permutation[q]) {
successors++;
}
}
index += (successors * factor);
factor *= position;
position++;
}
index = index + 1;
return index;
}
}
总耗时: 5756 ms
Python程序:
class Solution:
# @param {int[]} nums an integer array
# @return {long} a long integer
def permutationIndex(self, nums):
# Write your code here
index = 0
position = 2
factor = 1
numslen = len(nums)
for i in range((numslen-2),-1,-1):
successors = 0
for j in range((i+1),numslen):
if nums[i]>nums[j]:
successors+=1
index += successors*factor
factor*=position
position+=1
index +=1
return index
总耗时: 223 ms
同时,九章上面的程序还看不懂。。。
lintcode :Permutation Index 排列序号的更多相关文章
- lintcode Permutation Index
题目:http://www.lintcode.com/zh-cn/problem/permutation-index/ 排列序号 给出一个不含重复数字的排列,求这些数字的所有排列按字典序排序后该排列的 ...
- Lintcode: Permutation Index II
Given a permutation which may contain repeated numbers, find its index in all the permutations of th ...
- [OJ] Permutation Index
LintCode 197. Permutation Index (Easy) LintCode 198. Permutation Index II (Medium) 感觉这两道题主要考察计算排列组合的 ...
- Permutation Index I & II
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
- GridView控件中加自动排列序号
GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField HeaderText="序号"> < ...
- * 197. Permutation Index【LintCode by java】
Description Given a permutation which contains no repeated number, find its index in all the permuta ...
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- [CareerCup] 9.3 Magic Index 魔法序号
9.3 A magic index in an array A[0.. .n-1] is defined to be an index such that A[i] = i. Given a sort ...
- [LintCode] Permuation Index
Given a permutation which contains no repeated number, find its index in all the permutations of the ...
随机推荐
- Hibernate中的对象状态,及自动更新原因
Hibernate的对象有三种状态,分别为:瞬时状态 (Transient). 持久化状态(Persistent).游离状态(Detached).对它的深入理解,才能更好的理解hibernate的运行 ...
- js实现checkbox的全选/取消
所有的操作都将使用jquery进行. 主要是为了实现指定内容的批量/单独删除操作. 先看一下页面的设计. 实现操作的主要地方是: 首先实现单击“标题”旁的checkbox实现所有条目的选择. 要点:j ...
- 另辟思路解决 Android 4.0.4 不能监听Home键的问题
问题描述: 自从Android 4.0以后,开发人员是不能监听和屏蔽Home键的,对于KEYCODE_HOME,官方给出的描述如下: Home key. This key is handled by ...
- crontab的应用
当我们需要定时执行某个系统内的php脚本程序时,可以这样设置crontab * 19 * * * /usr/local/php/bin/php /var/www/test.php 此处表示调用php( ...
- 在Web API中使用Swagger-UI开源组件(一个深坑的解决)
介绍: Swagger-Ui是一个非常棒的Web API说明帮助页,具体详情可自行Google和百度. 官网:http://swagger.io/ GitHub地址:https://github ...
- Java Web应用中调优线程池的重要性
不论你是否关注,Java Web应用都或多或少的使用了线程池来处理请求.线程池的实现细节可能会被忽视,但是有关于线程池的使用和调优迟早是需要了解的.本文主要介绍Java线程池的使用和如何正确的配置线程 ...
- MVC 使用 FluentScheduler 定时器计划任务
MVC 使用 FluentScheduler 定时器计划任务 MacBook Pro 只有四个 USB Type-C 接口是否错了? 一项新技术的诞生总会对已存在的事物造成冲击或影响,如果大家都害怕冲 ...
- Struts2结合sitemesh3制作网站母版页面
上一篇文章介绍了sitemesh3的使用,这篇文章来介绍如何结合struts2来配置和使用sitemesh,具体的如何使用sitemesh3我就不讲解了,这个你们可以看看我的上一篇博客. 首先你要添加 ...
- Unity3D 利用NGUI2.6.3做技能冷却的CD效果
转自http://blog.csdn.net/qqmcy/article/details/9469021 NGUI非常强大我们今天来学习一下,如何利用NGUI做技能冷却的CD效果.先导入NGUI的插件 ...
- Codeforces Round #327 (Div. 2) E. Three States
题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...