LeetCode //C - 60. Permutation Sequence

60. 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 for n = 3:

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the k t h k^{th} kth permutation sequence.

Example 1:

Input: n = 3, k = 3
Output: "213"

Example 2:

Input: n = 4, k = 9
Output: "2314"

Example 3:

Input: n = 3, k = 1
Output: ""123"

Constraints:
  • 1 <= n <= 9
  • 1 <= k <= n!

From: LeetCode

Link: 60. Permutation Sequence


Solution:

Ideas:
  1. Factorial Calculation: The function starts by calculating factorials, which helps in determining which block or set of permutations the desired permutation falls into.
  2. Position Calculation: By dividing k by the factorial of n−1, the function determines the index of the number to place in each position of the resultant string.
  3. Update and Shift: After determining the position, the selected number is removed from the available list, effectively reducing the problem size for the next iteration.
  4. Memory Management: The function dynamically allocates memory for the result string and a temporary array to hold available numbers, ensuring to free the temporary memory before returning.
Code:
c 复制代码
// Helper function to calculate factorial
int factorial(int x) {
    int result = 1;
    for (int i = 2; i <= x; i++) {
        result *= i;
    }
    return result;
}

// Function to get the k-th permutation sequence
char* getPermutation(int n, int k) {
    int i, j, f;
    int len = n;
    k--; // Convert k to zero-indexed for easier calculations

    // Allocate memory for the result
    char *result = malloc((n + 1) * sizeof(char));
    result[n] = '\0'; // Null terminate the string

    // Create an array to hold numbers 1, 2, 3, ..., n
    int *numbers = malloc(n * sizeof(int));
    for (i = 0; i < n; i++) {
        numbers[i] = i + 1;
    }

    for (i = 0; i < len; i++) {
        f = factorial(n - 1);
        j = k / f; // Determine the index of the current digit
        result[i] = numbers[j] + '0'; // Set the current position in result
        k %= f; // Reduce k

        // Remove used number from the array by shifting elements
        for (int m = j; m < n - 1; m++) {
            numbers[m] = numbers[m + 1];
        }
        n--;
    }

    // Clean up and return result
    free(numbers);
    return result;
}
相关推荐
Dovis(誓平步青云)1 小时前
总用电下降了,为什么家庭能源看板仍然提示异常
java·开发语言·数据库·人工智能·算法·电脑·能源
开开心心就好1 小时前
A3的PDF怎么切成A4?拖一下分割线就行
服务器·开发语言·python·算法·pdf·ocr·tornado
SuperByteMaster1 小时前
while(1){;} 加分号
c语言
闻缺陷则喜何志丹1 小时前
详细设计说明书的内容要求
算法·测试·存储
Logic1012 小时前
C语言/数据结构位运算题解:异或XOR找出糖葫芦派对中的“独一无二山楂果数量“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
和裕2 小时前
定制纸箱刀卡、平卡、隔档分别是什么?结构与适用场景区别全解析
大数据·运维·网络·人工智能·算法
Tisfy2 小时前
LeetCode 3723.数位平方和的最大值:构造
数学·算法·leetcode·字符串·题解
hetao17338372 小时前
联盟场-提高组-R4 & ZYZSC-S-Round 7
c++·算法
星恒随风2 小时前
Linux进程信号(一):从Ctrl+C到kill、raise、alarm,深入理解信号的概念与产生原理
linux·c语言·笔记·学习·状态模式
碧海银沙音频科技研究院2 小时前
基于炬芯ATS3089X音箱的音乐播放歌词显示功能实现
人工智能·驱动开发·嵌入式硬件·算法