📌  相关文章
📜  在最多进行K次连续交换之后,按词典顺序排列的最小数组

📅  最后修改于: 2021-04-29 10:16:17             🧑  作者: 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 kn )中选取最小的元素[当k较小时考虑k ,否则n]。将所有这些元素右移1个位置后,将最小的元素放置在0位置。我们从k中减去掉期数(掉期数是班次数减去1)。如果仍然使k> 0,则从下一个起始位置应用相同的过程,即a 2 ,a 3 …(a kn ),然后将其放置在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
?>


Output: 2 7 6 9 1

时间复杂度: O(N 2 )
辅助空间: O(1)

参考:
http://stackoverflow.com/questions/25539423/finding-minimal-lexicographical-array