60. Permutation Sequence -Medium

本文介绍了一种高效算法,用于求解给定n和k时的第k个排列序列。通过对全排列规律的分析,利用数学方法快速定位序列中的每一个数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Question

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.

给出一个集合[1, 2, 3, …, n],它是一个连续序列,其按以下顺序得到所有序列的结果(上面的1-6的顺序)。现在给出n和k,请你返回第K个序列

Example

None

Solution

  • 首先如果暴力回溯解决,我们可以遍历每个结果,然后返回第K个元素,当然耗时过长。所以我们需要运用一些技巧。我们知道,由n个字符组成的字符串按照不同的顺序共有n!种,即n * (n - 1)!。假设我们有如下序列{1, 2, 3, 4},共有4! = 24种字符串

    1 + {2, 3, 4}

    2 + {1, 3, 4}

    3 + {1, 2, 4}

    4 + {1, 2, 3}

    所以我们可以确定第K个序列(实际上K=K-1,因为第一个数的下标索引为0)的第一个字符为 K / (n - 1)!,比如K=13,那么12 / 6 = 2,它代表下标为2的字符是第K个序列的第一个字符,而确定完第一个字符后,我们只需更新K = K % (n - 1),然后仍然按照之前的思想来判断第二个字符直到所有字符都判断完

    public class Solution {
        public String getPermutation(int n, int k) {
            // nums = {1, 2, 3, ..., n},nums[i]代表第i个元素
            List<Integer> nums = new ArrayList<>();
            for(int i = 0; i < n; i++) nums.add(i + 1);
    
            // factorial = {1, 1, 2, 6, ..., (n - 1)!},factorial[i]表示n个字符的组合总数
            int[] factorial = new int[n];
            factorial[0] = 1;
            for(int i = 1; i < n; i++) factorial[i] = factorial[i - 1] * i;
    
            k -= 1;
            // 因为下标为0时代表第一个数,所以k需要减1
            StringBuilder sb = new StringBuilder();
            // 确定每一位的字符
            for(int i = n; i > 0; i--){
                // factorial[i - 1]代表(i - 1)!
                int index = k / factorial[i - 1];
                sb.append(nums.get(index));
                // 因为每次的index确定的是第几个元素为当前元素,所以为了确保不会重复添加相同元素要删去该元素
                nums.remove(index);
                // 更新K
                k = k % factorial[i - 1];
            }
            return sb.toString();
        }
    }
with_progress({ + # 初始化进度条 + p <- progressor(along = 1:2) + + # 执行100次置换 + placebo_dist <- manual_placebo(data, n_permute = 2) + }) One outcome and one treatment time found. Running single_augsynth. One outcome and one treatment time found. Running single_augsynth. Warning message: In handle_progression(condition, debug = debug) : Received a progression ‘update’ request (amount=1; msg=‘character(0)’) but is not listening to this progressor. This can happen when code signals more progress updates than it configured the progressor to do. When the progressor completes all steps, it shuts down resulting in the global progression handler to no longer listen to it. To troubleshoot this, try with progressr::handlers("debug") > # 清理内存碎片 > gc() used (Mb) gc trigger (Mb) max used (Mb) Ncells 2983096 159.4 5026872 268.5 5026872 268.5 Vcells 10326783 78.8 36845423 281.2 112127404 855.5 > # 5. 结果查看 ------------------------------------------------------------------ > # 查看前10次置换结果 > print(head(placebo_dist, 10)) Time Estimate lower_bound upper_bound p_val permutation pseudo_unit 2016-04-01...1 2016-04-01 -0.18368508 NA NA NA 1 1 2016-04-02...2 2016-04-02 -0.31366935 NA NA NA 1 1 2016-04-03...3 2016-04-03 -0.49916072 NA NA NA 1 1 2016-04-04...4 2016-04-04 0.30554081 NA NA NA 1 1 2016-04-05...5 2016-04-05 -0.33573735 NA NA NA 1 1 2016-04-06...6 2016-04-06 0.07909620 NA NA NA 1 1 2016-04-07...7 2016-04-07 -0.21757939 NA NA NA 1 1 2016-04-08...8 2016-04-08 -0.45763920 NA NA NA 1 1 2016-04-09...9 2016-04-09 0.07273611 NA NA NA 1 1 2016-04-10...10 2016-04-10 0.17557989 NA NA NA 1 1 >
最新发布
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值