Byte urine sex, Cantor expands and seeks the Kth arrangement!

Byte urine sex, Cantor expands and seeks the Kth arrangement!


Today is the 109th day of Xiaohao's algorithm "365 Questions Program". Continue to explain to you the 60th question of leetcode, which is a medium difficulty question.

There are two main problems with arranging categories, one is full arrangement:

Can Xiaobai really learn the full permutation algorithm by reading an article?

The other is this question (both questions appeared in Jianzhi offer):

Byte urine sex, Cantor expands and seeks the Kth arrangement!

01

PART

Kth permutation


The topic is more convoluted, you can still understand it with patience~ Come on!



Problem: Given a set [1,2,3,...,n], all its elements have n! permutations.

List all arrangements in order of size, and mark them one by one. When n = 3, all arrangements are as follows:

"123"

"132"

"213"

"231"

"312"

"321"

Given n and k, return the kth permutation.

Description:

The range of given n is [1, 9].

The range of given k is [1, n!].


Give two examples:

Example 1:

Input: n = 3, k = 3 Output: "213"

Byte urine sex, Cantor expands and seeks the Kth arrangement!

Example 2:

Input: n = 4, k = 9 Output: "2314"

02

PART

Problem solution analysis


The label of this question is mathematics and backtracking algorithm, so we use math and backtracking methods to solve the problem respectively.


Starting from the mathematical method, let's talk about the Cantor expansion.

So what does Kantor unfold? It is used to calculate the order of the current arrangement in all arrangements from small to complete . Fuck, isn't it this question?

Don't understand? That is to say, if you know that 1234 is the serial number 1, and 1243 is the serial number 2, this formula can directly tell you what the serial number of 4132 is!

The formula is this:

Byte urine sex, Cantor expands and seeks the Kth arrangement!

Explain one:

This X is the final serial number value, which is one bit less than the actual serial number, because you can see that the first digit calculated by Cantor's expansion is 0.

Byte urine sex, Cantor expands and seeks the Kth arrangement!

Many pictures seen on the Internet may be ranked from 1. Please pay attention to this:

Byte urine sex, Cantor expands and seeks the Kth arrangement!

The key is this Byte urine sex, Cantor expands and seeks the Kth arrangement!. In fact, it is to see which number i of the original number ranks among the elements that are not currently appearing.

I feel that this sentence is a bit confusing. Explain with the question above: 1234 is the serial number 1, 1243 is the serial number 2, and what is the serial number of 4132?

Explanation: The first number is 4, there are 3 numbers that are less than 4 and have not appeared yet (123), the second number is 1, and the numbers that are less than 1 and have not appeared are 0. The third number is 3, and the number that is less than 3 and has not appeared is 1. The fourth number is 2, and the number that is less than 2 and has not appeared is 0.

There is X = 3 3! + 0 2! + 1 1! + 0 0! = 19, the serial number at this time is 19+1 = 20. If you don't understand, look at the following picture:

Byte urine sex, Cantor expands and seeks the Kth arrangement!

Then we have the opposite of the above things come, it is called the inverse Cantor started. In other words, it gives us the value of X, let us ask.

For the inverse Cantor expansion, I still give an example. For example, if nums is still 1234, we want to find the 15th position. Then take the following steps:

  • First, because X is one bit less than the actual serial number, we need to subtract 1 from the actual serial number, which is 15-1 = 14.

  • The first character of the target value, 14 / 3! = 2 ... 2 (quotient 2 remaining 2), there are no used characters, and the first character is the third in increasing order among the unused characters. Ie 3

  • The second character of the target value, 2 / 2! = 1 ... 0, the used character is 3, and the second character is the second in increasing order among the unused characters. Ie 2

  • The third character of the target value, 0 / 1! = 0 ... 0, the used characters are 2 and 3, and the third character is the first in increasing order among the unused characters. I.e. 1

  • The third character of the target value, 0 / 0! = 0 ... 0, the used characters are 1, 2, and 3, and the fourth character is ranked first in increasing order among the unused characters. I.e. 4

  • Then the required sequence is: 3214.

  • You can check the above table and find that it is correct. If you don’t understand, you can go back to the above example and take a look again. In fact, it’s just to reverse the above process.

Finally, we will apply the inverse Cantor:

//JAVA
class Solution {
    public String getPermutation(int n, int k) {
        StringBuilder sb = new StringBuilder();
        // 候选数字
        List<Integer> candidates = new ArrayList<>();
        // 分母的阶乘数
        int[] factorials = new int[n+1];
        factorials[0] = 1;
        int fact = 1;
        for(int i = 1; i <= n; ++i) {
            candidates.add(i);
            fact *= i;
            factorials[i] = fact;
        }
        //预处理
        k -= 1;
        for(int i = n-1; i >= 0; --i) {
            // 计算候选数字的index
            int index = k / factorials[i];
            sb.append(candidates.remove(index));
            k -= index*factorials[i];
        }
        return sb.toString();
    }
}

Byte urine sex, Cantor expands and seeks the Kth arrangement!

03

PART

Similar topics


It is said that it is a similar problem, but in fact, I used backtracking to solve the following two problems. It's a general solution~ If you are interested, you can use backtracking to solve this problem.

Xiaobai learns to arrange everything

Xiaobai learns to find subsets (Kaishou)

I have compiled all the solutions I wrote into an e-book, each with a complete illustration.

Guess you like

Origin blog.51cto.com/15076236/2609722