Skip to content

Commit

Permalink
New Problem Solution - "1850. Minimum Adjacent Swaps to Reach the Kth…
Browse files Browse the repository at this point in the history
… Smallest Number"
  • Loading branch information
haoel committed May 3, 2021
1 parent 83fa31a commit fb308ad
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1850|[Minimum Adjacent Swaps to Reach the Kth Smallest Number](https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/) | [C++](./algorithms/cpp/minimumAdjacentSwapsToReachTheKthSmallestNumber/MinimumAdjacentSwapsToReachTheKthSmallestNumber.cpp)|Medium|
|1849|[Splitting a String Into Descending Consecutive Values](https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/) | [C++](./algorithms/cpp/splittingAStringIntoDescendingConsecutiveValues/SplittingAStringIntoDescendingConsecutiveValues.cpp)|Medium|
|1848|[Minimum Distance to the Target Element](https://leetcode.com/problems/minimum-distance-to-the-target-element/) | [C++](./algorithms/cpp/minimumDistanceToTheTargetElement/MinimumDistanceToTheTargetElement.cpp)|Easy|
|1847|[Closest Room](https://leetcode.com/problems/closest-room/) | [C++](./algorithms/cpp/closestRoom/ClosestRoom.cpp)|Hard|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// Source : https://leetcode.com/problems/minimum-adjacent-swaps-to-reach-the-kth-smallest-number/
// Author : Hao Chen
// Date : 2021-05-03

/*****************************************************************************************************
*
* 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".
*
* 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 <= 1000
* 1 <= k <= 1000
* num only consists of digits.
******************************************************************************************************/

class Solution {
private:
// Refer to:
// https://leetcode.com/problems/next-permutation/solution/
void nextPermutation(string& num) {
int i = num.size() - 2;
while (i >= 0 && num[i + 1] <= num[i]) {
i--;
}
if (i >= 0) {
int j = num.size() - 1;
while (j >= 0 && num[j] <= num[i]) {
j--;
}
swap(num[i], num[j]);
}
reverse(num, i + 1);
}

void reverse(string& num, int start) {
int i = start, j = num.size() - 1;
while (i < j) {
swap(num[i], num[j]);
i++;
j--;
}
}


public:
int getMinSwaps(string num, int k) {
string pnum = num;
while(k--) {
nextPermutation(pnum);
}
//cout << num << endl << pnum << endl;
int result = 0;
for(int i = 0; i < num.size(); i++) {
if (num[i] == pnum[i]) continue;
for(int j = i + 1; j < num.size(); j++) {
if(num[i] != pnum[j]) continue;
//cout << "j=" << j << ", i=" << i << endl;
result += j - i;

//shift the string
char c = pnum[j];
for (int k = j; k > i; k--) {
pnum[k] = pnum[k-1];
}
pnum[i] = c;
//cout << pnum << endl;
break;
}
}
//cout << endl;
return result;
}
};

0 comments on commit fb308ad

Please sign in to comment.