Written question 73. Leetcode OJ (60)

Source: Internet
Author: User

Permutation Sequence

This question is the k sequence of the full permutation of the numbers for 1~n (n[1~9]).

The general idea is: Use a counter, recursive return to find the whole sequence, find a counter plus one, until the first K.

But add if n = 9 I'm looking for the first (9!) -1) The number, then the above method of the time is how much, will probably time out (did not try, but I am sure that will be timed out, because this idea is not advisable), think we only need a sequence, do not need to find out all the sequence. Below I give a solution to the problem, I personally feel is desirable.

We are the people who have studied mathematics and want us to perfection the nth sequence of the arrangement, so we can actually speculate that it is roughly distributed in that range (not with the eyes, but deduced). Here's how it's done:

Assuming n = 3, then the full arrangement of the three elements and the corresponding K-sequence atmosphere are as follows:

K n[1,3]
1 1 2 3
2 1 3 2
3 2 1 3
4 2 3 1
5 3 1 2
6 3 2 1

Let's start by explaining the full permutation pattern like this non-repeating number:

n = 1 o'clock, the total number of permutations is: 1!

n = 2 o'clock, the total number of permutations is: 2!

n = 3 o'clock, the total number of permutations is: 3!

.....

based on this law we can roughly speculate on the scope of K, the method is to find a number m, when m! > K, so that can explain the approximate position of K, draw a picture of the idea.


The above-mentioned problem-solving method is really not well described, because it is the law of numbers, I filter some places to continuously reduce the value of k, when reduced to K = 1 when the corresponding sequence is the sequence we are looking for, if not understand it is I did not describe a good question. So I teach one way:

For example:

When: n = 2

1 2

2 1

Then we exchange 1 and 2 position (Exchange first and second position), it is equivalent to filter out a sequence, the value of K will be reduced by 1

When: n = 3

1 2 3

1 3 2

2 1 3

2 3 1

3 1 2

3 2 1

So when we swapped positions 1 and 2, we filtered 2 sequences, and the corresponding k values were reduced by 2, and the problem was narrowed down to finding the K sequence in the full array of two numbers .

When we swapped positions 1 and 3, we filtered out 4 sequences, the corresponding k values were reduced by 4, and the problem was reduced to finding the K virtual column in the full array of two numbers .

.... So this can be deduced to n = 4,5,6,7 ..., the problem will continue to shrink, eventually reduced to 1, problem solving, code as follows, can refer to code understanding.

Class Solution {public:string getpermutation (int n, int k) {//1~n the full array of K-numbers string ret;ret.clear (); if (n = = 1 && k ! = 1 | | K > Factorial (n)) {return ret;} Vector<int> v;v.clear (); for (int i = 1; I <= n; ++i) {v.push_back (i);}  int begin = 0;int pos = 0;while (k > 0) {begin = 1;int offset = 0;int Nums = 0;while ((nums = factorial (begin)) < K) { K is distributed after the first begin bit + + begin;}  Nums/= begin;pos = n-begin; The POS bit may need to be exchanged if (Begin > N+1) {//does not exist, actually is out of bounds, meaning K ratio n! Also large, there is no return ret;}  int temp = 0;int jump = 0; Go to the while (Temp+nums < k) {//To find the need and the swap location temp + = Nums;++jump;//jump indicates the relative distance of the position}if (Jump > 0) {swap (V[pos], V[pos+ju            MP]);//Swap position after order sort (V.begin () + pos + 1, v.end ()) is required for the subsequent sequence;} After swapping, the relative position of k-= Jump*nums;if (k <= 1) {//This condition indicates that this is the sequence to be searched for (int i=0;i<n;++i) {ret.push_back (V[i] + ' 0 ');} return ret;}} return ret;} int factorial (int n) {//for factorial int ret = 1;for (int i = 2; I <= n; ++i) {ret *= i;} return ret;}};
The results are as follows:


Written question 73. Leetcode OJ (60)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.