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;
}
相关推荐
linx29511 分钟前
第七章 · 标准库容器、算法与 ranges
c语言·开发语言·数据结构·c++·算法
钓鱼的肝15 分钟前
csp-j-s总结(4)
c++·经验分享·笔记·算法
Logic10127 分钟前
C语言/数据结构位运算题解:异或XOR找出独特数字的索引位置——成对数字在两侧
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
橘子汽水16828 分钟前
Leetcode 763,45 划分字母区间 跳跃游戏II
数据结构·算法·leetcode
天天喝旺仔1 小时前
Git 内部原理深度解析:从 blob/tree/commit 对象到 packfile 与垃圾回收
数据结构·数据库·git·算法·哈希
AIGCmagic社区1 小时前
KITTI AbsRel从6.5压到5.4,Marigold V2用一张32GB卡把编辑DiT收成单步深度估计
人工智能·算法·aigc·ai多模态
青少儿编程课堂1 小时前
多源最短路与最小环(Floyd 算法图论解析)
c++·python·算法·bfs·信息学竞赛
6Hzlia1 小时前
【Classic 150 刷题计划】 LeetCode 228. 汇总区间 | C++ 锚点游标与断点检测法
c++·算法·leetcode
Edward The Bunny2 小时前
Leetcode Hot 100
数据结构·算法
hans汉斯2 小时前
数据挖掘|基于BP神经网络的少数民族村寨文化型旅游体验产品潜在游客挖掘
深度学习·神经网络·算法·yolo·软件工程·bp·汉斯出版社