1850. Minimum Adjacent Swaps to Reach the Kth Smallest Number
https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number
Description
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.
For example, when
num = "5489355142"
:The 1st smallest wonderful integer is
"5489355214"
.The 2nd smallest wonderful integer is
"5489355241"
.The 3rd smallest wonderful integer is
"5489355412"
.The 4th smallest wonderful integer is
"5489355421"
.
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.
Example 1:
Example 2:
Example 3:
Constraints:
2 <= num.length <= 1000
1 <= k <= 1000
num
only consists of digits.
ac
Last updated