随笔- 509  文章- 0  评论- 151  阅读- 22万 

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\n", solution.getPermutation(n, k).c_str());
62     }
63     
64     return 0;
65 }
66 #endif
复制代码

 

 posted on   zhuli19901106  阅读(231)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示