zoukankan      html  css  js  c++  java
  • [LeetCode#60]Permutation Sequence

    Problem:

    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:

    Once you understand the mathematical principle behind this problem, the idea could be very easy.
    To successfully implement out the solution, you must equip good programming skills.
    Basic idea:
    According to the example, the small number should be placed before large number at first. which means:
    For num1 (num2 ... num9), 
    There should be permutations among range (num2 ... num9), before we change num1. 
    Apparently there are 8! permutations in the range(num2 ... num9). This is the same for every num in the combination. 
    
    By taking advantage of this truth, we could calcualte the index for each position. Suppose k start from 0. 
    index[0] = k/(n - 1)!
    index[1] = k'/(n - 2)! Note: k' = k % (n - 1)
    
    Skill
    1. To use divide and mod operation, we make k start from 0. 
    Benefit: the calculated index could be directly used for locating numbers. 
    k--;
    
    2. Use ArrayList for recording the available nums. (This method is very elegant! By taking the self adjustment property of the ArrayList, we no longer need to record which num was already used or not.) 
    for (int i = 1; i <= n; i++) {
        num.add(i);
    }
    ...
    while (round >= 0) {
        int index = k / factorial;
        k %= factorial;
        ret.append(num.get(index));
        num.remove(index);
        if (round > 0)
            factorial /= round;
        round --; 
    }
    
    Note: 
    index = k / factorial;
    The index must in the range of [0, round] when enter the while. 
    Cause:
    k %= factorial; <factorial is (round+1)>
    the k is in the range of [(round-1)!, (round)!]
    
    3. Get initial factorial, adjust it along the loop. 
    a. get (n-1)!
    for (int i = 2; i <= n - 1; i++) {
        factorial *= i;
    }
    b. adjust it for next round. 
    if (round > 0)
        factorial /= round;
    round --; 
    c. get current index, and prepare k for getting next index
    int index = k / factorial;
    k %= factorial;
    
    This problem is not hard, but the programming skills it requires are not easy. 
    
    Time complexity:
    Apparently, we need to computate the index for n factorial from (n to 1). For each computation, there is a append and remove operation. the remove operation would take O(n), thus the total time complexity is O(n^2).

    Solution:

    public class Solution {
        public String getPermutation(int n, int k) {
            if (n <= 0 || n > 9)
                return "";
            k--;
            StringBuffer ret = new StringBuffer();
            ArrayList<Integer> num = new ArrayList<Integer> ();
            int factorial = 1; 
            for (int i = 1; i <= n; i++) {
                num.add(i);
            }
            for (int i = 2; i <= n - 1; i++) {
                factorial *= i;
            }
            int round = n - 1;
            while (round >= 0) {
                int index = k / factorial;
                k %= factorial;
                ret.append(num.get(index));
                num.remove(index);
                //avoid the case at last round
                if (round > 0)
                    factorial /= round;
                round --; 
            }
            return ret.toString();
        }
    }
  • 相关阅读:
    Linux之Ubuntu添加/移除个人软件包存档的源[PPA,Personal Package Archives]
    [C++]Linux之头文件sys/types.h[/usr/include/sys]
    [C++]Linux之文件拷贝在系统调用和C库函数下的效率比较
    Linux之Ubuntu下安装屏幕录像软件(SimpleScreenRecorder)【摘抄】
    [C++]基于Curses库的实时系统监测可视化系统-2017-12-09 15-07-42
    [C++]Linux之虚拟文件系统[/proc]中关于CPU/内存/网络/内核等的一些概要性说明
    [C++]Linux之计算内存利用率与辨析
    [C++]Linux之网络实时检测功能
    [C++]Linux之C编程异常[true未定义解决方案]
    [C++]Linux之读取计算机网络数据[/proc/net/dev]
  • 原文地址:https://www.cnblogs.com/airwindow/p/4746741.html
Copyright © 2011-2022 走看看