📌  相关文章
📜  如果 K 更大,则通过交换 K 和 arr[i] 来最小化对给定数组进行排序的操作

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

如果 K 更大,则通过交换 K 和 arr[i] 来最小化对给定数组进行排序的操作

给定一个包含N个整数的数组arr[]和一个整数K ,任务是找到以非递减顺序对数组进行排序所需的最小操作数,以便在每个操作中任何数组元素arr[i]都可以交换K如果(arr[i] > K)的值。

例子:

方法:给定的问题可以使用贪心方法来解决,这个想法是在每个步骤中最小化arr[i]的值,对于范围[0, N – 1]内的所有i ,这是进一步的最优选择要排序的数组。因此,如果arr[i] > K的值,交换arr[i]K的值是最佳选择。请按照以下步骤解决给定的问题:

  • 创建一个变量cnt ,它存储执行的操作的计数。最初cnt = 0
  • 使用[0, N-1]范围内的变量ii的递增顺序遍历数组arr[]
  • 对于每个索引,如果arr[i] > K ,交换Karr[i]的值并将cnt的值增加1
  • 每次操作后,使用本文讨论的方法检查数组arr[]是否排序。如果数组arr[]已排序,则返回cnt的值作为所需答案。
  • 如果执行上述步骤后数组未排序,则打印-1

下面是上述方法的实现:

C++
// C++ program of the above approach
 
#include 
using namespace std;
 
// Function to find the minimum number
// of given operations in order to sort
// the array arr[] in non-decreasing order
int minimumswaps(int arr[], int N, int K)
{
    // If arr[] is already sorted, return 0
    if (is_sorted(arr, arr + N)) {
        return 0;
    }
 
    // Stores the count of operations
    int cnt = 0;
 
    // Loop to iterate over the array
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is greater than K,
        // minimize the value of arr[i]
        if (arr[i] > K) {
            swap(arr[i], K);
 
            // Increment the count by 1
            cnt++;
 
            // Check if the array is sorted
            // after the last operation
            if (is_sorted(arr, arr + N)) {
 
                // Return answer
                return cnt;
            }
        }
    }
 
    // Not Possible to sort the array using
    // given operation, hence return -1
    return -1;
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 2, 3, 5, 4 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 1;
 
    cout << minimumswaps(arr, N, K);
 
    return 0;
}


Java
// Java program of the above approach
import java.io.*;
class GFG
{
    static boolean is_sorted(int arr[], int N)
    {
        for (int i = 0; i < N - 1; i++)
        {
 
            if (arr[i] > arr[i + 1])
                return false;
        }
 
        return true;
    }
 
    // Function to find the minimum number
    // of given operations in order to sort
    // the array arr[] in non-decreasing order
    static int minimumswaps(int arr[], int N, int K)
    {
       
        // If arr[] is already sorted, return 0
        if (is_sorted(arr, N)) {
            return 0;
        }
 
        // Stores the count of operations
        int cnt = 0;
 
        // Loop to iterate over the array
        for (int i = 0; i < N; i++) {
 
            // If arr[i] is greater than K,
            // minimize the value of arr[i]
            if (arr[i] > K) {
                int temp = arr[i];
                  arr[i] = K;
                  K = temp;
 
                // Increment the count by 1
                cnt++;
 
                // Check if the array is sorted
                // after the last operation
                if (is_sorted(arr, N)) {
 
                    // Return answer
                    return cnt;
                }
            }
        }
 
        // Not Possible to sort the array using
        // given operation, hence return -1
        return -1;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 0, 2, 3, 5, 4 };
        int N = arr.length;
           int K = 1;
 
        System.out.println(minimumswaps(arr, N, K));
    }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python 3 program of the above approach
def is_sort(arr):
    for i in range(len(arr)-1):
        if arr[i]>arr[i+1]:
            return False
    return True
   
# Function to find the minimum number
# of given operations in order to sort
# the array arr[] in non-decreasing order
def minimumswaps(arr, N, K):
   
    # If arr[] is already sorted, return 0
    if is_sort(arr):
        return 0
 
    # Stores the count of operations
    cnt = 0
 
    # Loop to iterate over the array
    for i in range(N):
        # If arr[i] is greater than K,
        # minimize the value of arr[i]
        if(arr[i] > K):
            temp = arr[i]
            arr[i] = K
            K = temp
             
            # Increment the count by 1
            cnt += 1
 
            # Check if the array is sorted
            # after the last operation
            if is_sort(arr):
                # Return answer
                return cnt
 
    # Not Possible to sort the array using
    # given operation, hence return -1
    return -1
 
# Driver Code
if __name__ == '__main__':
    arr = [0, 2, 3, 5, 4]
    N = len(arr)
    K = 1
    print(minimumswaps(arr, N, K))
     
    # This code is contributed by bgangwar59.


C#
// C# program of the above approach
using System;
class GFG {
    static bool is_sorted(int[] arr, int N)
    {
        for (int i = 0; i < N - 1; i++) {
 
            if (arr[i] > arr[i + 1])
                return false;
        }
 
        return true;
    }
 
    // Function to find the minimum number
    // of given operations in order to sort
    // the array arr[] in non-decreasing order
    static int minimumswaps(int[] arr, int N, int K)
    {
 
        // If arr[] is already sorted, return 0
        if (is_sorted(arr, N)) {
            return 0;
        }
 
        // Stores the count of operations
        int cnt = 0;
 
        // Loop to iterate over the array
        for (int i = 0; i < N; i++) {
 
            // If arr[i] is greater than K,
            // minimize the value of arr[i]
            if (arr[i] > K) {
                int temp = arr[i];
                arr[i] = K;
                K = temp;
 
                // Increment the count by 1
                cnt++;
 
                // Check if the array is sorted
                // after the last operation
                if (is_sorted(arr, N)) {
 
                    // Return answer
                    return cnt;
                }
            }
        }
 
        // Not Possible to sort the array using
        // given operation, hence return -1
        return -1;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int[] arr = { 0, 2, 3, 5, 4 };
        int N = arr.Length;
        int K = 1;
 
        Console.WriteLine(minimumswaps(arr, N, K));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
3

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