LeetCode 1850: Minimum Adjacent Swaps to Reach the Kth Smallest Number Solution
Master LeetCode problem 1850 (Minimum Adjacent Swaps to Reach the Kth Smallest Number), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1850. Minimum Adjacent Swaps to Reach the Kth Smallest Number
Problem Explanation
Explanation
To solve this problem, we need to find the kth smallest wonderful number that is greater than the given number num. We can achieve this by generating all permutations of the digits in num and then sorting them to find the kth smallest wonderful number.
To reduce the number of swaps needed, we can follow these steps:
- Find the position where the current number differs from the kth smallest wonderful number.
- Use a greedy approach to swap digits to move the differing digit to its correct position in the kth smallest wonderful number.
The time complexity of this approach is O(n^2 * n!), where n is the length of the input number num, and the space complexity is O(n) for storing the digits.
Solution Code
class Solution {
public int getMinSwaps(String num, int k) {
char[] original = num.toCharArray();
char[] target = num.toCharArray();
for (int i = 0; i < k; i++) {
nextPermutation(target);
}
int swaps = 0;
for (int i = 0; i < original.length; i++) {
if (original[i] != target[i]) {
int j = i + 1;
while (original[j] != target[i]) j++;
while (j > i) {
char temp = original[j];
original[j] = original[j - 1];
original[j - 1] = temp;
j--;
swaps++;
}
}
}
return swaps;
}
private void nextPermutation(char[] nums) {
int i = nums.length - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i >= 0) {
int j = nums.length - 1;
while (j >= 0 && nums[j] <= nums[i]) {
j--;
}
swap(nums, i, j);
}
reverse(nums, i + 1);
}
private void swap(char[] nums, int i, int j) {
char temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
private void reverse(char[] nums, int start) {
int i = start;
int j = nums.length - 1;
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1850 (Minimum Adjacent Swaps to Reach the Kth Smallest Number)?
This page provides optimized solutions for LeetCode problem 1850 (Minimum Adjacent Swaps to Reach the Kth Smallest Number) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1850 (Minimum Adjacent Swaps to Reach the Kth Smallest Number)?
The time complexity for LeetCode 1850 (Minimum Adjacent Swaps to Reach the Kth Smallest Number) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1850 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1850 in Java, C++, or Python.