Leetcode first _ Permutation Sequence

Source: Internet
Author: User

This question is quite good. If your idea is to generate a full arrangement every time and accumulate k times, stop it and it will definitely time out ..

This year, Microsoft wrote a similar question. As I have mentioned before, it only contains strings 0 and 1. What is the k-th arrangement. This question is more difficult than that, but the general idea is the same. Assume that n numbers are arranged in the k-th order. Like filling in a table, fill in from high to status one by one. First, consider the number of N-1 numbers to be arranged. A maximum of (n-1 )! In this case, when the nth number is added, the nth number can be increased from 1 to n. If the nth number is not increased by 1, the total number of contained arrays will increase (n-1 )!, Therefore, if k/(n-1) is used )!, The result is the number that should appear in the ascending order. In order to calculate what the following bit should be filled in, k should be updated to k % (n-1 )!. Calculate the I-bit should be filled in k/(I-1 )!. No, this is not just the case. It should be a big difference between this question and the 01 question. When filling in the I-digit, You should also check the remaining numbers, find the k/(I-1) in the remaining number )! .

Why do we need to reduce k by 1 in the code, which can be understood by a simple example. Just like in a matrix, the k number in the matrix is given, which requires that it correspond to the position in the matrix and will first subtract one from it. The value range of the involved number is given in the question. Therefore, you can calculate the factorial and put it in the array.

class Solution {public:    string getPermutation(int n, int k) {        int fac[10];        bool vis[10];        memset(vis, 0, sizeof(vis));        fac[0] = 1;        for(int i=1;i<10;i++)            fac[i] = fac[i-1]*i;        string res(n, '0');        --k;        for(int i=n-1;i>=0;i--){            int temp = k/fac[i];            int j=1;            for(;j<10;j++){                if(vis[j] == 0)                    temp--;                if(temp<0)                    break;            }            res[n-i-1] = '0'+j;            vis[j] = 1;            k%=fac[i];        }        return res;    }};



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.