zoukankan      html  css  js  c++  java
  • LeetCode

    Permutation Sequence

    2014.2.9 01:00

    The set [1,2,3,…,n] contains a total of n! unique permutations.

    By listing and labeling all of the permutations in order,
    We get the following sequence (ie, for n = 3):

    1. "123"
    2. "132"
    3. "213"
    4. "231"
    5. "312"
    6. "321"

    Given n and k, return the kth permutation sequence.

    Note: Given n will be between 1 and 9 inclusive.

    Solution:

      My first reaction on this problem was recursion. But soon I realized it would be a tail recursion.

      The problem can be solved in polynomial time.

      Let's consider a permutation of [1,n]. There're (n - 1)! permutations starting with one number from 1 to n. Thus an n-permutation starting with '4' has at least (4 - 1) * n! permutations before it. We swap '4' to the front and sort the remaing elements behind it in ascending order. In this manner, the problem is changed to (n - 1) scale. You know what to do next.

      Here is one demonstration for the process, the 23rd permutation of [1 2 3 4]:

        (23 is expressed as 22, zero-indexed)

        [1 2 3 4], 22

        [4 1 2 3], 22 / (3!) = 3, 22 % (3!) = 4

        [4 3 1 2], 4 / (2!) = 2, 4 % (2!) = 0

        [4 3 1 2], 0 / (1!) = 0, 0 % (1!) = 0

      Actually we don't really need to sort the remaining elements in O(n * log(n)) time. Shifting the elements one by one will keep the sequence sorted, and can be done in O(n) time.

      During the whole process, we need an array to store the permutation.

      Total time complexity is O(n^2), space complexity is O(n).

    Accepted code:

     1 // 1AC, good~
     2 // #define MY_MAIN
     3 #include <cstdio>
     4 #include <cstring>
     5 #include <string>
     6 #include <vector>
     7 using namespace std;
     8 
     9 class Solution {
    10 public:
    11     string getPermutation(int n, int k) {
    12         const int fac[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
    13         int i, j, tmp;
    14         int div;
    15         string result;
    16         
    17         --k;
    18         permutation.clear();
    19         for (i = 1; i <= n; ++i) {
    20             permutation.push_back(i);
    21         }
    22         
    23         for (i = n - 1; i >= 1; --i) {
    24             div = k / fac[i];
    25             tmp = permutation[n - 1 - i + div];
    26             for (j = 0; j < div; ++j) {
    27                 permutation[n - 1 - i + div - j] = permutation[n - 1 - i + div - j - 1];
    28             }
    29             permutation[n - 1 - i] = tmp;
    30             k = k % fac[i];
    31         }
    32         
    33         result = "";
    34         for (i = 0; i < n; ++i) {
    35             sprintf(s, "%d", permutation[i]);
    36             result = result + string(s);
    37         }
    38         permutation.clear();
    39         
    40         return result;
    41     }
    42 private:
    43     vector<int> permutation;
    44     char s[100];
    45 };
    46 
    47 #ifdef MY_MAIN
    48 int main()
    49 {
    50     Solution solution;
    51     int n, k;
    52     const int fac[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
    53     
    54     while (scanf("%d%d", &n, &k) == 2) {
    55         if (n < 1 || n > 9) {
    56             continue;
    57         }
    58         if (k < 1 || k > fac[n]) {
    59             continue;
    60         }
    61         printf("%s
    ", solution.getPermutation(n, k).c_str());
    62     }
    63     
    64     return 0;
    65 }
    66 #endif
  • 相关阅读:
    jenkins+docker+rancher+zikui 部署
    利用jenkins直接构件docker镜像并发布到docker服务器
    docker+Rancher+K3S
    windows使用VSCode进行Shell开发
    v-drag 弹框拖拽的实现
    vue3兄弟组件传值
    vue3 组件传值
    Azure Computer Vision 之 Smart Crop 智能裁剪图片
    ASP.NET Core 单元测试
    ASP.NET Core Static Files
  • 原文地址:https://www.cnblogs.com/zhuli19901106/p/3541187.html
Copyright © 2011-2022 走看看