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;
}
相关推荐
Stardep1 分钟前
算法入门19——二分查找算法——X的平方根
算法·leetcode·二分查找算法
We་ct2 分钟前
LeetCode 135. 分发糖果:双向约束下的最小糖果分配方案
前端·算法·leetcode·typescript
宇钶宇夕3 分钟前
CoDeSys入门实战一起学习(十三):函数(FUN)深度解析:自定义、属性与实操案例
运维·算法·自动化·软件工程
项目題供诗3 分钟前
C语言基础(十)
c语言·开发语言
代码游侠7 分钟前
学习笔记——GPIO按键与中断系统
c语言·开发语言·arm开发·笔记·嵌入式硬件·学习·重构
l1t8 分钟前
对clickhouse给出的二分法求解Advent of Code 2025第10题 电子工厂 第二部分的算法理解
数据库·算法·clickhouse
Tisfy10 分钟前
LeetCode 3315.构造最小位运算数组 II:位运算
算法·leetcode·题解·位运算
YuTaoShao23 分钟前
【LeetCode 每日一题】1292. 元素和小于等于阈值的正方形的最大边长
算法·leetcode·职场和发展
保护我方头发丶24 分钟前
hard_link.bat(个人用)
c语言
Remember_99324 分钟前
【数据结构】深入理解Map和Set:从搜索树到哈希表的完整解析
java·开发语言·数据结构·算法·leetcode·哈希算法·散列表