给定一个由N个整数组成的数组position [] ,其中position [i]表示第i个元素的位置,任务是通过执行以下两个操作之一来找到将所有元素移到同一位置所需的最低成本:
- 将位置[i]移到位置[i] + 2或位置[i] – 2 。费用= 0。
- 将表单position [i]移至position [i] + 1或position [i] – 1 。费用= 1。
例子:
Input: position[] = {1, 2, 3}
Output: 1
Explanation:
Operation 1: Move the element at position 3 to position 1. Cost = 0.
Operation 2: Move the element at position 2 to position 1. Cost = 1.
Therefore, total cost = 1.
Input: position[] = {2, 2, 2, 3, 3}
Output: 2
Explanation: Move the two elements at position 3 to position 2. Cost of each operation = 1. Therefore, total cost = 2.
方法:想法是遍历数组并计算奇数和偶数元素的数量。对于涉及两个索引递增或递减的每个操作,成本将始终为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)