📌  相关文章
📜  将所有元素移动到同一位置所需的最低成本

📅  最后修改于: 2021-04-29 16:32:33             🧑  作者: Mango

给定一个由N个整数组成的数组position [] ,其中position [i]表示第i元素的位置,任务是通过执行以下两个操作之一来找到将所有元素移到同一位置所需的最低成本:

  • 位置[i]移到位置[i] + 2位置[i] – 2 。费用= 0。
  • 将表单position [i]移至position [i] + 1position [i] – 1 。费用= 1。

例子:

方法:想法是遍历数组并计算奇数和偶数元素的数量。对于涉及两个索引递增或递减的每个操作,成本将始终为0。仅当元素从奇数位置移动到偶数位置,或反之亦然时,成本才会发生变化。因此,所需的最低成本是数组position []中存在的奇数和偶数元素的数量中的最小值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum
// cost required to place all
// elements in the same position
int minCost(int arr[], int arr_size)
{
     
    // Stores the count of even
    // and odd elements
    int odd = 0, even = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < arr_size; i++)
    {
         
        // Count even elements
        if (arr[i] % 2 == 0)
            even++;
             
        // Count odd elements
        else
            odd++;
    }
     
    // Print the minimum count
    cout << min(even, odd);
}
 
// Driver Code
int main()
{
     
    // Given array
    int arr[] = { 1, 2, 3 };
    int arr_size = sizeof(arr) / sizeof(arr[0]);
     
    // Function Call
    minCost(arr, arr_size);
}
 
// This code is contributed by khushboogoyal499


Java
// Java program to implement
// the above approach
 
import java.io.*;
class GFG {
 
    // Function to find the minimum
    // cost required to place all
    // elements in the same position
    public void minCost(int[] arr)
    {
        // Stores the count of even
        // and odd elements
        int odd = 0, even = 0;
 
        // Traverse the array arr[]
        for (int i = 0;
             i < arr.length;  i++) {
 
            // Count even elements
            if (arr[i] % 2 == 0)
                even++;
 
            // Count odd elements
            else
                odd++;
        }
 
        // Print the minimum count
        System.out.print(
            Math.min(even, odd));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        GFG obj = new GFG();
 
        // Given array
        int arr[] = { 1, 2, 3 };
 
        // Function Call
        obj.minCost(arr);
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to find the minimum
# cost required to place all
# elements in the same position
def minCost(arr):
     
    # Stores the count of even
    # and odd elements
    odd = 0
    even = 0
 
    # Traverse the array arr[]
    for i in range(len(arr)):
         
        # Count even elements
        if (arr[i] % 2 == 0):
            even += 1
             
        # Count odd elements
        else:
            odd += 1
 
    # Print the minimum count
    print(min(even, odd))
 
# Driver Code
if __name__ == '__main__':
     
    # Given array
    arr = [ 1, 2, 3 ]
 
    # Function Call
    minCost(arr)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach 
using System;
  
class GFG{
  
// Function to find the minimum
// cost required to place all
// elements in the same position
public void minCost(int[] arr)
{
     
    // Stores the count of even
    // and odd elements
    int odd = 0, even = 0;
 
    // Traverse the array arr[]
    for(int i = 0; i < arr.Length; i++)
    {
         
        // Count even elements
        if (arr[i] % 2 == 0)
            even++;
             
        // Count odd elements
        else
            odd++;
    }
     
    // Print the minimum count
    Console.Write(Math.Min(even, odd));
}
 
// Driver Code
public static void Main()
{
    GFG obj = new GFG();
     
    // Given array
    int[] arr = { 1, 2, 3 };
     
    // Function Call
    obj.minCost(arr);
}
}
 
// This code is contributed by sanjoy_62


输出:
1

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