Permutation Sequence

Given n and k, return the k-th permutation sequence.

 Notice

n will be between 1 and 9 inclusive.

Example

For n = 3, all permutations are listed as follows:

"123"
"132"
"213"
"231"
"312"
"321"

If k = 4, the fourth permutation is "231"

思路:from https://leetcode.com/problems/permutation-sequence/discuss/22507/%22Explain-like-I'm-five%22-Java-Solution-in-O(n)

say n = 4, we have {1, 2, 3, 4} 

If we list out all the permutations, we will get:

1 + (permutations of 2, 3, 4)
2 + (permutations of 1, 3, 4)
3 + (permutations of 1, 2, 4)
4 + (permutations of 1, 2, 3)

We know how to calculate the number of permutations of n numbers... n! So each of those with permutations of 3 numbers means there are 6 possible permutations. Meaning there would be a total of 24 permutations in this particular one. So if you were to look for the (k = 14) 14th permutation, it would be in the

3 + (permutations of 1, 2, 4) subset. 

To programmatically get that, you take k = 13 (subtract 1 because of things always starting at 0) and divide that by 6 we got from the factorial, which would give you the index of the number you want. In the array {1, 2, 3, 4}, k/(n-1)! = 13/(4-1)! = 13/3! = 13/6 = 2. The array {1, 2, 3, 4} has a value of 3 at index 2. So the first number is a 3.

Then the problem repeats with less numbers.

The permutations of {1, 2, 4} would be:

1 + (permutations of 2, 4)

2 + (permutations of 1, 4)

4 + (permutations of 1, 2)

But our k is no longer the 14th, because in the previous step, we've already eliminated the 12 4-number permutations starting with 1 and 2. So you subtract 12 from k which gives you 1. Programmatically that would be

k = k - (index from previous) * (n-1)! = k - 2*(n-1)! = 13 - 2*(3)! = 1

In this second step, permutations of 2 numbers has only 2 possibilities, meaning each of the three permutations listed above a has two possibilities, giving a total of 6. We're looking for the first one, so that would be in the 1 + (permutations of 2, 4) subset.

Meaning: index to get number from is k / (n - 2)! = 1 / (4-2)! = 1 / 2! = 0.. from {1, 2, 4}, index 0 is 1

so the numbers we have so far is 3, 1... and then repeating without explanations.

{2, 4}
k = k - (index from pervious) * (n-2)! = k - 0 * (n - 2)! = 1 - 0 = 1;
third number's index = k / (n - 3)! = 1 / (4-3)! = 1/ 1! = 1... from {2, 4}, index 1 has 4
Third number is 4

{2}
k = k - (index from pervious) * (n - 3)! = k - 1 * (4 - 3)! = 1 - 1 = 0;
third number's index = k / (n - 4)! = 0 / (4-4)! = 0/ 1 = 0... from {2}, index 0 has 2
Fourth number is 2

Giving us 3142. If you manually list out the permutations using DFS method, it would be 3142. Done! It really was all about pattern finding.

 

 1 public class Solution {
 2     public String getPermutation(int n, int k) {
 3         List<Integer> numberList = new ArrayList<>();
 4         for (int i = 1; i <= n; i++) {
 5             numberList.add(i);
 6         }
 7         k--;
 8         int factorial = calculateFactorial(n);
 9         StringBuilder sb = new StringBuilder();
10         for (int i = 0; i < n; i++) {
11             factorial = factorial / (n - i); // remove 1 number from group
12             int curIndex = k / factorial;   // find the right number(curIndex) of
13             sb.append(numberList.get(curIndex));   // get number according to curIndex
14             numberList.remove(curIndex);  // remove from list
15             k = k % factorial;   // update k
16         }
17         return sb.toString();
18     }
19     
20     private int calculateFactorial(int n) {
21         int result = 1;
22         for (int i = 1; i <= n; i++) {
23             result = result * i;
24         }
25         return result;
26     }
27 }

 

posted @ 2016-07-18 12:57  北叶青藤  阅读(245)  评论(0)    收藏  举报