📜  最多 K 个连续交换后的字典序最小数组

📅  最后修改于: 2021-10-26 05:14:22             🧑  作者: Mango

给定一个数组 arr[],找出在最多执行 k 次连续交换后可以获得的按字典顺序排列的最小数组。
例子 :

Input: arr[] = {7, 6, 9, 2, 1}
        k = 3
Output: arr[] = {2, 7, 6, 9, 1}
Explanation: Array is: 7, 6, 9, 2, 1
Swap 1:   7, 6, 2, 9, 1
Swap 2:   7, 2, 6, 9, 1
Swap 3:   2, 7, 6, 9, 1
So Our final array after k = 3 swaps : 
2, 7, 6, 9, 1

Input: arr[] = {7, 6, 9, 2, 1}
        k = 1
Output: arr[] = {6, 7, 9, 2, 1}
我们强烈建议您在继续解决方案之前单击此处进行练习。

朴素的方法是生成数组的所有排列并选择满足最多 k 次交换条件的最小排列。这种方法的时间复杂度是 Ω(n!),对于较大的 n 值肯定会超时。
一种有效的方法是贪婪地思考。我们首先从数组 a 1 , a 2 , a 3 …(a k or a n )中选取最小的元素[当 k 较小时我们考虑 a k ,否则为 n]。在将所有这些元素向右移动 1 个位置后,我们将最小的元素放置到 a 0位置。我们从 k 中减去交换次数(交换次数是移位次数减 1)。如果仍然剩下 k > 0,那么我们从下一个起始位置即 a 2 ,a 3 ,…(a k或 a n )应用相同的过程,然后将其放置到 a 1位置。所以我们继续应用相同的过程,直到 k 变为 0。

C++
// C++ program to find lexicographically minimum
// value after k swaps.
#include
using namespace std ;
 
// Modifies arr[0..n-1] to lexicographically smallest
// with k swaps.
void minimizeWithKSwaps(int arr[], int n, int k)
{
    for (int i = 0; i0; ++i)
    {
        // Set the position where we want
        // to put the smallest integer
        int pos = i;
        for (int j = i+1; j k)
                break;
 
            // Find the minimum value from i+1 to
            // max k or n
            if (arr[j] < arr[pos])
                pos = j;
        }
 
        // Swap the elements from Minimum position
        // we found till now to the i index
        for (int j = pos; j>i; --j)
            swap(arr[j], arr[j-1]);
 
        // Set the final value after swapping pos-i
        // elements
        k -=  pos-i;
    }
}
 
// Driver code
int main()
{
    int arr[] = {7, 6, 9, 2, 1};
    int n = sizeof(arr)/sizeof(arr[0]);
    int k = 3;
 
    minimizeWithKSwaps(arr, n, k);
 
    //Print the final Array
    for (int i=0; i


Java
// Java program to find lexicographically minimum
// value after k swaps.
class GFG {
     
    // Modifies arr[0..n-1] to lexicographically
    // smallest with k swaps.
    static void minimizeWithKSwaps(int arr[], int n, int k)
    {
        for (int i = 0; i < n-1 && k > 0; ++i)
        {
             
            // Set the position where we want
            // to put the smallest integer
            int pos = i;
            for (int j = i+1; j < n ; ++j)
            {
                 
                // If we exceed the Max swaps
                // then terminate the loop
                if (j - i > k)
                    break;
     
                // Find the minimum value from i+1 to
                // max k or n
                if (arr[j] < arr[pos])
                    pos = j;
            }
     
            // Swap the elements from Minimum position
            // we found till now to the i index
            int temp;
             
            for (int j = pos; j>i; --j)
            {
                temp=arr[j];
                arr[j]=arr[j-1];
                arr[j-1]=temp;
            }
             
            // Set the final value after swapping pos-i
            // elements
            k -= pos-i;
        }
    }
     
    // Driver method
    public static void main(String[] args)
    {
         
        int arr[] = {7, 6, 9, 2, 1};
        int n = arr.length;
        int k = 3;
     
        minimizeWithKSwaps(arr, n, k);
     
        //Print the final Array
        for (int i=0; i


Python
# Python program to find lexicographically minimum
# value after k swaps.
def minimizeWithKSwaps(arr, n, k):
 
    for i in range(n-1):
 
        # Set the position where we we want
    # to put the smallest integer
        pos = i
        for j in range(i+1, n):
 
            # If we exceed the Max swaps
        # then terminate the loop
            if (j-i > k):
                break
 
            # Find the minimum value from i+1 to
            # max (k or n)
            if (arr[j] < arr[pos]):
                pos = j
 
        # Swap the elements from Minimum position
        # we found till now to the i index
        for j in range(pos, i, -1):
            arr[j],arr[j-1] = arr[j-1], arr[j]
 
        # Set the final value after swapping pos-i
        # elements
        k -= pos - i
 
 
# Driver Code
n, k = 5, 3
arr = [7, 6, 9, 2, 1]
minimizeWithKSwaps(arr, n, k)
 
# Print the final Array
for i in range(n):
    print(arr[i], end = " ")


C#
// C# program to find lexicographically
// minimum value after k swaps.
using System;
 
class GFG {
     
    // Modifies arr[0..n-1] to lexicographically
    // smallest with k swaps.
    static void minimizeWithKSwaps(int []arr, int n,
                                              int k)
    {
        for (int i = 0; i < n-1 && k > 0; ++i)
        {
            // Set the position where we want
            // to put the smallest integer
            int pos = i;
            for (int j = i+1; j < n ; ++j)
            {
                // If we exceed the Max swaps
                // then terminate the loop
                if (j - i > k)
                    break;
     
                // Find the minimum value from
                // i + 1 to max k or n
                if (arr[j] < arr[pos])
                    pos = j;
            }
     
            // Swap the elements from Minimum position
            // we found till now to the i index
            int temp;
             
            for (int j = pos; j>i; --j)
            {
                temp=arr[j];
                arr[j]=arr[j-1];
                arr[j-1]=temp;
            }
             
            // Set the final value after
            // swapping pos-i elements
            k -= pos-i;
        }
    }
     
    // Driver method
    public static void Main()
    {
        int []arr = {7, 6, 9, 2, 1};
        int n = arr.Length;
        int k = 3;
         
        // Function calling
        minimizeWithKSwaps(arr, n, k);
     
        // Print the final Array
        for (int i=0; i


php
 0; ++$i)
    {
        // Set the position where we want
        // to put the smallest integer
        $pos = $i;
        for ($j = $i+1; $j < $n ; ++$j)
        {
            // If we exceed the Max swaps
            // then terminate the loop
            if ($j-$i > $k)
                break;
 
            // Find the minimum value from
            // i+1 to max k or n
            if ($arr[$j] < $arr[$pos])
                $pos = $j;
        }
 
        // Swap the elements from Minimum
        // position we found till now to
        // the i index
        for ($j = $pos; $j > $i; --$j)
        {
            $temp = $arr[$j];
            $arr[$j] = $arr[$j-1];
            $arr[$j-1] = $temp;
        }
         
        // Set the final value after
        // swapping pos-i elements
        $k -= $pos-$i;
    }
     
    //Print the final Array
    for ($i = 0; $i < $n; ++$i)
        echo $arr[$i] . " ";
}
 
// Driver code
$arr = array(7, 6, 9, 2, 1);
$n = count($arr);
$k = 3;
 
minimizeWithKSwaps($arr, $n, $k);
 
// This code is contributed by Sam007
?>


Javascript


Output: 2 7 6 9 1

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程