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;
}
相关推荐
软件算法开发3 分钟前
基于蘑菇繁殖优化的LSTM深度学习网络模型(MRO-LSTM)的一维时间序列预测算法matlab仿真
深度学习·算法·matlab·lstm·时间序列预测·蘑菇繁殖优化·mro-lstm
雪花desu4 分钟前
【Hot100-Java中等】LeetCode 11. 盛最多水的容器:双指针法的直观理解与数学证明
算法·leetcode
POLITE35 分钟前
Leetcode 438. 找到字符串中所有字母异位词 JavaScript (Day 4)
javascript·算法·leetcode
海绵宝龙10 分钟前
Vue 中的 Diff 算法
前端·vue.js·算法
_Voosk13 分钟前
macOS Xcode C++程序设置相对路径根目录
c语言·c++·xcode·swift
wadesir25 分钟前
高效计算欧拉函数(Rust语言实现详解)
开发语言·算法·rust
aini_lovee26 分钟前
基于扩展的增量流形学习算法IMM-ISOMAP的方案
算法
white-persist31 分钟前
【内网运维】Netsh 全体系 + Windows 系统专属命令行指令大全
运维·数据结构·windows·python·算法·安全·正则表达式
小刘爱玩单片机33 分钟前
【stm32简单外设篇】- LCD1602A
c语言·stm32·单片机·嵌入式硬件
超自然祈祷1 小时前
数据结构入门:图的基本操作、算法与 C++ 实现
算法·图搜索算法