📌  相关文章
📜  获得大于给定数字的第 K 个最小数字所需的最小相邻交换

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

获得大于给定数字的第 K 个最小数字所需的最小相邻交换

给定大小为N的数字字符串S和一个正整数K ,任务是找到S中所需的最小相邻交换次数,以获得大于给定字符串的第 K最小字符串字符串。

例子:

方法:给定的问题可以通过使用贪心方法来解决。请按照以下步骤解决问题:

  • 将当前数字字符串的副本存储在一个变量中,比如res
  • 创建一个变量,例如totalSwaps存储所需的最小交换。
  • 由于需要第 K 个最大的数,因此该语句等于从当前字符串开始找到第 K 个排列。
  • 使用函数next_permutation() 找到第 K 个排列。
  • 使用变量i遍历范围[0, N)并执行以下任务:
    • 如果res[i]不等于str[i]则将变量start初始化为i+1并遍历 while 循环直到res[i]不等于str[start]并将i的值增加1。
    • 迭代一个循环,直到i不等于start并交换值str[start]str[start – 1]并将start的值减少1并将totalSwaps的值增加1
  • 执行上述步骤后,打印totalSwaps的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the minimum number
// of swaps required to make the Kth
// smallest string greater than str
void minSwapsKthLargest(string str, int k)
{
    // Store the copy of the string
    string res = str;
 
    // Find the K-th permutation of
    // the given numeric string
    for (int i = 0; i < k; i++) {
        next_permutation(
            str.begin(), str.end());
      cout<<"str = "<


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG {
    static char[] next_permutation(char[] array) {
        int i = array.length - 1;
        while (i > 0 && array[i - 1] >= array[i]) {
            i--;
        }
 
        int j = array.length - 1;
 
        while (array[j] <= array[i - 1]) {
            j--;
        }
 
        char temp = array[i - 1];
        array[i - 1] = array[j];
        array[j] = temp;
 
        j = array.length - 1;
 
        while (i < j) {
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            i++;
            j--;
        }
 
        return array;
    }
 
    // Function to find the minimum number
    // of swaps required to make the Kth
    // smallest String greater than str
    static void minSwapsKthLargest(String str, int k)
    {
       
        // Store the copy of the String
        char[] res = str.toCharArray();
        char[] s = str.toCharArray();
       
        // Find the K-th permutation of
        // the given numeric String
        for (int i = 0; i < k; i++) {
            s = next_permutation(s);
        }
 
        // Stores the count of swaps required
        int swap_count = 0;
 
        // Traverse the String and find the
        // swaps required greedily
        for (int i = 0; i < res.length; i++) {
 
            // If both characters do not match
            if (res[i] != s[i]) {
                int start = i + 1;
 
                // Search from i+1 index
                while (res[i] != s[start]) {
 
                    // Find the index to swap
                    start++;
                }
                while (i != start) {
                    char t = s[start];
                    s[start] = s[start - 1];
                    s[start - 1] = t;
                    // Swap until the characters
                    // are at same index
                    start--;
                    swap_count++;
                }
            }
        }
 
        // Print the minimum number of counts
        System.out.print(swap_count);
    }
 
    // Driver Code
    public static void main(String[] args) {
        String S = "11112";
        int K = 4;
        minSwapsKthLargest(S, K);
 
    }
}
 
// This code is contributed by Rajput-Ji


C#
// C# program for the above approach
using System;
class GFG {
 
    static char[] next_permutation(char[] array)
    {
        int i = array.Length - 1;
        while (i > 0 && array[i - 1] >= array[i]) {
            i--;
        }
 
        int j = array.Length - 1;
 
        while (array[j] <= array[i - 1]) {
            j--;
        }
 
        char temp = array[i - 1];
        array[i - 1] = array[j];
        array[j] = temp;
 
        j = array.Length - 1;
 
        while (i < j) {
            temp = array[i];
            array[i] = array[j];
            array[j] = temp;
            i++;
            j--;
        }
 
        return array;
    }
 
    // Function to find the minimum number
    // of swaps required to make the Kth
    // smallest string greater than str
    static void minSwapsKthLargest(string str, int k)
    {
        // Store the copy of the string
        string res = str;
        char[] str1 = str.ToCharArray();
 
        // Find the K-th permutation of
        // the given numeric string
        for (int i = 0; i < k; i++) {
            next_permutation(str1);
        }
 
        // Stores the count of swaps required
        int swap_count = 0;
 
        // Traverse the string and find the
        // swaps required greedily
        for (int i = 0; i < res.Length; i++) {
 
            // If both characters do not match
            if (res[i] != str1[i]) {
                int start = i + 1;
 
                // Search from i+1 index
                while (res[i] != str1[start]) {
 
                    // Find the index to swap
                    start++;
                }
                while (i != start) {
                    char temp = str1[start];
                    str1[start] = str1[start - 1];
                    str1[start - 1] = temp;
 
                    // Swap until the characters
                    // are at same index
                    start--;
                    swap_count++;
                }
            }
        }
 
        // Print the minimum number of counts
        Console.WriteLine(swap_count);
    }
 
    // Driver Code
    public static void Main()
    {
        string S = "11112";
        int K = 4;
        minSwapsKthLargest(S, K);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
4

时间复杂度: O(N*(N + K))
辅助空间: O(N)