📜  最多 K 个连续交换的最大字典数组

📅  最后修改于: 2021-10-26 02:27:48             🧑  作者: Mango

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

Input : arr[] = {3, 5, 4, 1, 2}
        k = 3
Output : 5, 4, 3, 2, 1
Explanation : Array given : 3 5 4 1 2
After swap 1 : 5 3 4 1 2
After swap 2 : 5 4 3 1 2
After swap 3 : 5 4 3 2 1

Input : arr[] = {3, 5, 1, 2, 1}
        k = 3
Output : 5, 3, 2, 1, 1

蛮力方法:生成数组的所有排列,然后选择满足最多 K 次交换条件的排列。这种方法的时间复杂度是O(n!)
优化方法:在这种贪婪方法中,首先找到数组中存在的最大元素,该元素大于(如果第一个位置元素不是最大的)第一个位置,并且最多可以用 K 次交换放置在第一个位置.找到那个元素后,记下它的索引。然后,交换数组的元素并更新 K 值。将此过程应用于其他位置,直到 k 非零或数组变为字典序最大。
以下是上述方法的实现:

C++
// C++ program to find lexicographically
// maximum value after k swaps.
#include 
using namespace std;
 
// Function which modifies the array
void KSwapMaximum(int arr[], int n, int k)
{
    for (int i = 0; i < n - 1 && k > 0; ++i) {
 
        // Here, indexPosition is set where we
        // want to put the current largest integer
        int indexPosition = i;
        for (int j = i + 1; j < n; ++j) {
 
            // If we exceed the Max swaps
            // then break the loop
            if (k <= j - i)
                break;
 
            // Find the maximum value from i+1 to
            // max k or n which will replace
            // arr[indexPosition]
            if (arr[j] > arr[indexPosition])
                indexPosition = j;
        }
 
        // Swap the elements from Maximum indexPosition
        // we found till now to the ith index
        for (int j = indexPosition; j > i; --j)
            swap(arr[j], arr[j - 1]);
 
        // Updates k after swapping indexPosition-i
        // elements
        k -= indexPosition - i;
    }
}
 
// Driver code
int main()
{
    int arr[] = { 3, 5, 4, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
 
    KSwapMaximum(arr, n, k);
 
    // Print the final Array
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
}


Java
// Java program to find
// lexicographically
// maximum value after
// k swaps.
import java.io.*;
 
class GFG
{
    static void SwapInts(int array[],
                         int position1,
                         int position2)
    {
        // Swaps elements
        // in an array.
         
        // Copy the first
        // position's element
        int temp = array[position1];
         
        // Assign to the
        // second element
        array[position1] = array[position2];
         
        // Assign to the
        // first element
        array[position2] = temp;
    }
     
    // Function which
    // modifies the array
    static void KSwapMaximum(int []arr,
                             int n, int k)
    {
        for (int i = 0;
                 i < n - 1 && k > 0; ++i)
        {
     
            // Here, indexPosition
            // is set where we want to
            // put the current largest
            // integer
            int indexPosition = i;
            for (int j = i + 1; j < n; ++j)
            {
     
                // If we exceed the
                // Max swaps then
                // break the loop
                if (k <= j - i)
                    break;
     
                // Find the maximum value
                // from i+1 to max k or n
                // which will replace
                // arr[indexPosition]
                if (arr[j] > arr[indexPosition])
                    indexPosition = j;
            }
     
            // Swap the elements from
            // Maximum indexPosition
            // we found till now to
            // the ith index
            for (int j = indexPosition; j > i; --j)
                SwapInts(arr, j, j - 1);
     
            // Updates k after swapping
            // indexPosition-i elements
            k -= indexPosition - i;
        }
    }
     
    // Driver code
    public static void main(String args[])
    {
        int []arr = { 3, 5, 4, 1, 2 };
        int n = arr.length;
        int k = 3;
     
        KSwapMaximum(arr, n, k);
     
        // Print the final Array
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
    }
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


Python3
# Python program to find
# lexicographically
# maximum value after
# k swaps.
 
arr = [3, 5, 4, 1, 2]
 
# Function which
# modifies the array
def KSwapMaximum(n, k) :
     
    global arr
    for i in range(0, n - 1) :
        if (k > 0) :
         
            # Here, indexPosition
            # is set where we want to
            # put the current largest
            # integer
            indexPosition = i
            for j in range(i + 1, n) :        
     
                # If we exceed the Max swaps
                # then break the loop
                if (k <= j - i) :
                    break
     
                # Find the maximum value
                # from i+1 to max k or n
                # which will replace
                # arr[indexPosition]
                if (arr[j] > arr[indexPosition]) :
                    indexPosition = j
             
            # Swap the elements from
            # Maximum indexPosition
            # we found till now to
            # the ith index
            for j in range(indexPosition, i, -1) :
                t = arr[j]
                arr[j] = arr[j - 1]
                arr[j - 1] = t
     
            # Updates k after swapping
            # indexPosition-i elements
            k = k - indexPosition - i
 
# Driver code
n = len(arr)
k = 3
 
KSwapMaximum(n, k)
 
# Print the final Array
for i in range(0, n) :
    print ("{} " .
            format(arr[i]),
                 end = "")
     
# This code is contributed by
# Manish Shaw(manishshaw1)


C#
// C# program to find
// lexicographically
// maximum value after
// k swaps.
using System;
 
class GFG
{
    static void SwapInts(int[] array,
                         int position1,
                         int position2)
    {
        // Swaps elements in an array.
         
        // Copy the first position's element
        int temp = array[position1];
         
        // Assign to the second element
        array[position1] = array[position2];
         
        // Assign to the first element
        array[position2] = temp;
    }
     
    // Function which
    // modifies the array
    static void KSwapMaximum(int []arr,
                             int n, int k)
    {
        for (int i = 0;
                 i < n - 1 && k > 0; ++i)
        {
     
            // Here, indexPosition
            // is set where we want to
            // put the current largest
            // integer
            int indexPosition = i;
            for (int j = i + 1; j < n; ++j)
            {
     
                // If we exceed the
                // Max swaps then
                // break the loop
                if (k <= j - i)
                    break;
     
                // Find the maximum value
                // from i+1 to max k or n
                // which will replace
                // arr[indexPosition]
                if (arr[j] > arr[indexPosition])
                    indexPosition = j;
            }
     
            // Swap the elements from
            // Maximum indexPosition
            // we found till now to
            // the ith index
            for (int j = indexPosition; j > i; --j)
                SwapInts(arr, j, j - 1);
     
            // Updates k after swapping
            // indexPosition-i elements
            k -= indexPosition - i;
        }
    }
     
    // Driver code
    static void Main()
    {
        int []arr = new int[]{ 3, 5, 4, 1, 2 };
        int n = arr.Length;
        int k = 3;
     
        KSwapMaximum(arr, n, k);
     
        // Print the final Array
        for (int i = 0; i < n; ++i)
            Console.Write(arr[i] + " ");
    }
}
// This code is contributed by
// Manish Shaw(manishshaw1)


PHP
 0; $i++)
    {
         
        // Here, indexPosition
        // is set where we want to
        // put the current largest
        // integer
        $indexPosition = $i;
        for ($j = $i + 1;
             $j < $n; $j++)
        {
 
            // If we exceed the Max swaps
            // then break the loop
            if ($k <= $j - $i)
                break;
 
            // Find the maximum value
            // from i+1 to max k or n
            // which will replace
            // arr[indexPosition]
            if ($arr[$j] > $arr[$indexPosition])
                $indexPosition = $j;
        }
         
        // Swap the elements from
        // Maximum indexPosition
        // we found till now to
        // the ith index
        for ($j = $indexPosition;
             $j > $i; $j--)
            swap($arr[$j], $arr[$j - 1]);
 
        // Updates k after swapping
        // indexPosition-i elements
        $k -= $indexPosition - $i;
    }
}
 
// Driver code
$arr = array( 3, 5, 4, 1, 2 );
$n = count($arr);
$k = 3;
 
KSwapMaximum($arr, $n, $k);
 
// Print the final Array
for ($i = 0; $i < $n; $i++)
    echo ($arr[$i]." ");
     
// This code is contributed by
// Manish Shaw(manishshaw1)
?>


Javascript


输出:
5 4 3 1 2

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

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