给定一个由N 个整数组成的数组position[] ,其中position[i]表示第i个元素的位置,任务是通过执行以下两个操作之一来找到将所有元素移动到同一位置所需的最小成本:
- 将表单position[i]移动到position[i] + 2或position[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
Javascript
1
时间复杂度: O(N)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live