📌  相关文章
📜  使 N 可被 K 整除所需的最小相邻数字交换

📅  最后修改于: 2022-05-13 01:56:05.913000             🧑  作者: Mango

使 N 可被 K 整除所需的最小相邻数字交换

给定两个整数NK ,任务是计算使整数N可被K整除所需的最小相邻数字交换次数。

例子:

方法:给定问题可以通过迭代给定整数的所有数字排列并检查所有可被 K 整除的整数来解决,K 是将给定整数转换为当前整数所需的最小相邻交换数。以下是要遵循的步骤:

  • 将给定的整数转换为字符串str ,并按非降序字符字符排序。
  • 使用内置的 next_permutation()函数遍历str的所有排列。
  • 如果当前排列表示的整数可以被K整除,请检查使用此算法将N转换为当前整数所需的交换次数。
  • 保持变量中所需的最小交换次数,这是所需的答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum number of
// swaps requires to convert s1 to s2
int CountSteps(string s1, string s2, int size)
{
    int i = 0, j = 0;
    int result = 0;
 
    // Iterate over the first string
    // and convert every element
    while (i < size) {
        j = i;
 
        // Find index element of which
        // is equal to the ith element
        while (s1[j] != s2[i]) {
            j += 1;
        }
 
        // Swap adjacent elements in
        // the first string
        while (i < j) {
 
            // Swap elements
            char temp = s1[j];
            s1[j] = s1[j - 1];
            s1[j - 1] = temp;
            j -= 1;
            result += 1;
        }
        i += 1;
    }
    return result;
}
 
// Function to find minimum number of adjacent
// swaps required to make N divisible by K
int swapDigits(int N, int K)
{
    // Convert the integer into string
    string str = to_string(N);
 
    // Sort the elements of the string
    sort(str.begin(), str.end());
 
    // Stores the count of swaps
    int ans = INT_MAX;
 
    // Iterate over all permutations
    // of the given string
    do {
        // If the current integer
        // is divisible by K
        if (stoi(str) % K == 0)
 
            // Update ans
            ans = min(ans,
                      CountSteps(to_string(N), str,
                                 str.length()));
 
    } while (next_permutation(str.begin(),
                              str.end()));
 
    // Return Answer
    return ans;
}
 
// Driver Code
int main()
{
    int N = 10203456;
    int K = 100;
    cout << swapDigits(N, K);
 
    return 0;
}


Python3
# Python 3 program for the above approach
import sys
 
# Function for next permutation
def next_permutation(arr):
 
    # Find the length of the array
    n = len(arr)
 
    # Start from the right most digit and
    # find the first digit that is smaller
    # than the digit next to it.
    k = n - 2
    while k >= 0:
        if arr[k] < arr[k + 1]:
            break
 
        k -= 1
 
    # Reverse the list if the digit that
    # is smaller than the digit next to
    # it is not found.
    if k < 0:
        arr = arr[::-1]
    else:
        # Find the first greatest element
        # than arr[k] from the end of the list
        for l in range(n - 1, k, -1):
            if arr[l] > arr[k]:
                break
 
        # Swap the elements at arr[k] and arr[l
        arr[l], arr[k] = arr[k], arr[l]
 
        # Reverse the list from k + 1 to the end
        # to find the most nearest greater number
        # to the given input number
        arr[k + 1:] = reversed(arr[k + 1:])
 
    return arr
 
# Function to find minimum number of
# swaps requires to convert s1 to s2
def CountSteps(s1,  s2,  size):
 
    i = 0
    j = 0
    result = 0
 
    # Iterate over the first string
    # and convert every element
    while (i < size):
        j = i
        # Find index element of which
        # is equal to the ith element
        while (s1[j] != s2[i]):
            j += 1
 
        # Swap adjacent elements in
        # the first string
        while (i < j):
 
            # Swap elements
            temp = s1[j]
            s1[j] = s1[j - 1]
            s1[j - 1] = temp
            j -= 1
            result += 1
        i += 1
    return result
 
# Function to find minimum number of adjacent
# swaps required to make N divisible by K
def swapDigits(N,  K):
 
    # Convert the integer into string
    st = str(N)
    st2 = str(N)
 
    # Sort the elements of the string
    st = list(st)
    st2 = list(st2)
    st.sort()
    st2.sort()
 
    # Stores the count of swaps
    ans = sys.maxsize
 
    # Iterate over all permutations
    # of the given string
    # If the current integer
    # is divisible by K
    while (next_permutation(st) != st2):
        if(int(''.join(st)) % K == 0):
            ans = min(ans,
                      CountSteps(list(str(N)), st,
                                 len(st)))
 
    # Return Answer
    return ans
   
# Driver Code
if __name__ == "__main__":
 
    N = 10203456
    K = 100
    print(swapDigits(N, K))
 
    # This code is contributed by ukasp.



输出
9

时间复杂度: O((log N)! * (log N) 2 )
辅助空间: O(log N)