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.

Example

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小的有两个数。全排列可以以它们开头。

 1 public class Solution {
 2     public long permutationIndex(int[] A) {
 3         long index = 0, fact = 1;
 4         for (int i = A.length - 1; i >= 0; i--) {
 5             int numOfSmaller = 0;
 6             for (int j = i + 1; j < A.length; j++) {
 7                 if (A[j] < A[i]) numOfSmaller++;  // numOfSmaller refers to the numbers which can begin with;
 8             }
 9             index += numOfSmaller * fact;
10             fact *= (A.length - i);
11         }
12         return index + 1;
13     }
14 }

Permutation Index II

Given a permutation which may contain repeated numbers, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.

Example

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! = 144index是所有previous排列的次数和,返回下一次index+1

 1 import java.util.HashMap;
 2
 3 public class Solution {
 4     public long permutationIndexII(int[] A) {
 5         long index = 0, fact = 1, dup = 1;
 6         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
 7         for (int i = A.length - 1; i >= 0; i--) {
 8             if (!map.containsKey(A[i])) {
 9                 map.put(A[i], 1);
10             } else {
11                 map.put(A[i], map.get(A[i]) + 1);
12                 dup *= map.get(A[i]);
13             }
14             int numOfSmallers = 0;
15             for (int j = i + 1; j < A.length; j++) {
16                 if (A[j] < A[i])
17                     numOfSmallers++;
18             }
19             index += numOfSmallers * fact / dup;
20             fact *= (A.length - i);
21         }
22         return index + 1;
23     }
24 }
时间: 2024-11-10 07:59:39

Permutation Index I & II的相关文章

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

lintcode 容易题:Permutation Index 排列序号

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

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大

[LeetCode] Find Permutation 找全排列

By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. And our secret signature was constructed by a s

Word Search 和 Word Search Ⅱ

Word Search 和 Word Search Ⅱ Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighbo

OPEN CASCADE BSpline Curve Interpolation

OPEN CASCADE BSpline Curve Interpolation [email protected] Abstract. Global curve interpolation to point data is a way to construct curves. The paper focus on the interpolate algorithm in OPEN CASCADE, and give a simple example to demonstrate the usa

u-boot中网口处理--硬件部分

1.  网口硬件方案: AT91SAM9G10 + DM9000CEP: DM9000CEP为MAC+PHY解决方案,与MCU链接通过8位或16位数据总线. 内部SRAM为16Kbyte. 2. DM9000CEP硬件接口(略). 3. DM9000CEP寄存器. DM9000CEP包含两类寄存器:控制状态寄存器(CSRs)和PHY寄存器. 访问PHY寄存器是通过访问CSRs实现的,相关寄存器有: 1) EPCR:EEPROm&PHY Control Register. 2) EPAR : EE

去除预加载的Viewpager

package com.example.zhbj_heima47.view; /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtai

把《c++ primer》读薄(4-2 c和c++的数组 和 指针初探)

督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1.我们知道,将一个数组赋给另一个数组,就是将一个数组的元素逐个赋值给另一数组的对应元素,相应的,将一个vector 赋给另一个vector,也是将一个vector 的元素逐个赋值给另一vector 的对应元素: //将一个vector 赋值给另一vector,使用迭代器访问vector 中的元素 vector<int> ivec(10, 20); vector<int> ivec1; for (vecto