Practice Minimum Adjacent Swaps to Reach the Kth Smallest Number
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Minimum Adjacent Swaps to Reach the Kth Smallest Number
Problem Statement
You are given a string num, representing a large integer, and an integer k. We call some integer wonderful if it is a permutation of the digits in num and is greater in value than num. There can be many wonderful integers. However, we only care about the smallest-valued ones. Return the minimum number of adjacent digit swaps that needs to be applied to num to reach the kth smallest wonderful integer. The tests are generated in such a way that kth smallest wonderful integer exists.
Source: Minimum Adjacent Swaps to Reach the Kth Smallest Number on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
num = "5489355142", k = 4
2
# Example 2
num = "11112", k = 4
4
# Example 3
num = "00123", k = 1
1
Constraints
Related LeetCode Problems
Practice more problems similar to Minimum Adjacent Swaps to Reach the Kth Smallest Number.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.