Minimum Adjacent Swaps to Reach the Kth Smallest Number Problem
Description
LeetCode Problem 1850.
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 1^st smallest wonderful integer is
"5489355214". - The 2^nd smallest wonderful integer is
"5489355241". - The 3^rd smallest wonderful integer is
"5489355412". - The 4^th smallest wonderful integer is
"5489355421".
- The 1^st smallest wonderful integer is
Return the minimum number of adjacent digit swaps that needs to be applied to num to reach the k^th* smallest wonderful integer*.
The tests are generated in such a way that k^th smallest wonderful integer exists.
Example 1:
Input: num = "5489355142", k = 4
Output: 2
Explanation: The 4^th smallest wonderful number is "5489355421". To get this number:
- Swap index 7 with index 8: "5489355142" -> "5489355412"
- Swap index 8 with index 9: "5489355412" -> "5489355421"
Example 2:
Input: num = "11112", k = 4
Output: 4
Explanation: The 4^th smallest wonderful number is "21111". To get this number:
- Swap index 3 with index 4: "11112" -> "11121"
- Swap index 2 with index 3: "11121" -> "11211"
- Swap index 1 with index 2: "11211" -> "12111"
- Swap index 0 with index 1: "12111" -> "21111"
Example 3:
Input: num = "00123", k = 1
Output: 1
Explanation: The 1^st smallest wonderful number is "00132". To get this number:
- Swap index 3 with index 4: "00123" -> "00132"
Constraints:
2 <= num.length <= 10001 <= k <= 1000numonly consists of digits.
Solution
Approach: Next Permutation and Inversions
First apply next permutation k times to get the target arrangement s.
Then count the adjacent swaps that turn num into s. If all digits were distinct, replace each digit by its position in num and write s in those positions; the swaps needed are the number of inversions in that array. With repeated digits, give the equal digits of s the positions of that digit in num in order of appearance (matching them in order never costs more). Count the inversions with a double loop (or a Fenwick tree).
Complexity: O(n(k + n)) time and O(n) space, where n is the length of num.
class Solution:
def getMinSwaps(self, num: str, k: int) -> int:
def next_permutation(nums: List[str]) -> bool:
n = len(nums)
i = n - 2
while i >= 0 and nums[i] >= nums[i + 1]:
i -= 1
if i < 0:
return False
j = n - 1
while j >= 0 and nums[j] <= nums[i]:
j -= 1
nums[i], nums[j] = nums[j], nums[i]
nums[i + 1 : n] = nums[i + 1 : n][::-1]
return True
s = list(num)
for _ in range(k):
next_permutation(s)
d = [[] for _ in range(10)]
idx = [0] * 10
n = len(s)
for i, c in enumerate(num):
j = ord(c) - ord("0")
d[j].append(i)
arr = [0] * n
for i, c in enumerate(s):
j = ord(c) - ord("0")
arr[i] = d[j][idx[j]]
idx[j] += 1
return sum(arr[j] > arr[i] for i in range(n) for j in range(i))