leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

1850.cpp (449B)


      1 class Solution {
      2   public:
      3     int getMinSwaps(string &num, int k) {
      4         string perm = num;
      5         while (k--)
      6             next_permutation(perm.begin(), perm.end());
      7 
      8         int res = 0;
      9         for (int i = 0, j = 0; i < num.size(); j = ++i) {
     10             while (num[j++] != perm[i])
     11                 ;
     12             res += j - i - 1;
     13             while (i < --j)
     14                 swap(num[j], num[j - 1]);
     15         }
     16         return res;
     17     }
     18 };