[LeetCode]Permutation Sequence

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.

Analysis:

A better way is to fully use the properties of permutation: the total number of permutation with length n is n!.
First, let's take a look at how permutation works:

Major steps are swap and fix:
Here in order to grow the tree,  every time start the first unfixed element in each node, generate child nodes by swapping the first element with every other element.The leave nodes are those do not have element to swap. 

So, when we have the idea of how to generate the permutation, next step is to generate it faster. Think the tree in this way:

                         ABC
         /                   |                    \
      ABC              BAC               CBA
    /       \             /      \               /    \
ABC  ACB     BAC BCA    CBA CAB

Every leave node is a permutation. Take a look at the second level, each subtree (second level nodes as the root), there are (n-1)! permutations in it.  Totally there are n nodes in 2nd level, thus the total number of permutations are n*(n-1)!=n!.

So, what we want to do is to locate one permutation among the leave nodes. The simple method is to generate and search each leave node until we find the one. Now, what we can do here we want to skip some trees to reduce the complexity. e.g. in the above tree, if we know the required leave node is in the third sub tree (under CBA), we can skip the first two and search from the "CBA".

Say we have the required permutation is the kth one, first we can locate which subtree it belongs to in the 2nd level, by computing s = k / ((n-1)!). Then we get the sth subtree, and set k=k%((n-1)!) , now search the sub tree until we get the leave node. How to get the sth subtree root node? Just like the idea of how permutation works (the first figure): Just put the sth elment after fixed letter.  e.g. ABCD,  we want the 3rd subtree root node in 2nd level, just put C in the 1st place, which is CABD;   For ABCDE, we want the 3rd subtree root node in the 3rd level, it is ADBCE.

Java

public String getPermutation(int n, int k) {
	int []num = new int[n];
		int permCount = 1;
		for(int i=0;i<n;i++){
			num[i] = i+1;
			permCount*=(i+1);
		}
		k--;
		StringBuilder target = new StringBuilder();
		for(int i=0;i<n;i++){
			permCount = permCount/(n-i);
			int choosed = k/permCount;
			target.append(num[choosed]);
			for(int j=choosed;j<n-i-1;j++){
				num[j] = num[j+1];
			}
			k = k%permCount;
		}
		return target.toString();
    }

c++

string getPermutation(int n, int k) {
        vector<int>num(n);
		int permCount = 1;
		for(int i=0;i<n;i++){
			num[i] = i+1;
			permCount*=(i+1);
		}
		k--;
		string target;
		for(int i=0;i<n;i++){
			permCount = permCount/(n-i);
			int choosed = k/permCount;
			target.push_back(num[choosed]+'0');
			for(int j=choosed;j<n-i;j++){
				num[j] = num[j+1];
			}
			k = k%permCount;
		}
		return target;
    }






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值