lintcode-medium-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.

public class Solution {
    /**
     * @param A an integer array
     * @return a long integer
     */
    public long permutationIndexII(int[] A) {
        // Write your code here

        if(A == null || A.length == 0)
            return 1;

        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

        for(int i = 0; i < A.length; i++){
            if(map.containsKey(A[i]))
                map.put(A[i], map.get(A[i]) + 1);
            else
                map.put(A[i], 1);
        }

        long result = 1;

        for(int i = 0; i < A.length; i++){
            HashSet<Integer> set = new HashSet<Integer>();

            for(int j = i + 1; j < A.length; j++){
                if(A[j] < A[i] && !set.contains(A[j])){
                    set.add(A[j]);

                    map.put(A[j], map.get(A[j]) - 1);
                    result += getNum(map);
                    map.put(A[j], map.get(A[j]) + 1);
                }
            }

            map.put(A[i], map.get(A[i]) - 1);
        }

        return result;
    }

    public long factor(int n){
        long result = 1;

        for(int i = 1; i <= n; i++)
            result = result * i;

        return result;
    }

    public long getNum(HashMap<Integer, Integer> map){
        long den = 1;
        int count = 0;

        for(Integer temp: map.values()){
            if(temp != 0){
                count += temp;
                den *= factor(temp);
            }
        }

        return factor(count) / den;
    }

}
时间: 2024-10-07 06:07:57

lintcode-medium-Permutation Index 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 medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

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/

[poj] The Wedding Juicer | [lintcode] Trapping Rain Water II

问题描述 给定一个二维矩阵,每个元素都有一个正整数值,表示高度.这样构成了一个二维的.有高度的物体.请问该矩阵可以盛放多少水? 相关题目:POJ The Wedding Juicer Description Farmer John's cows have taken a side job designing interesting punch-bowl designs. The designs are created as follows: * A flat board of size W cm

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大

lintcode medium Best Time to Buy and Sell Stock I,II,III

Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

Palindrome Permutation I &amp; II

Palindrome Permutation I Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even