📌  相关文章
📜  最小化使所有数组元素大于或等于零所需的成本

📅  最后修改于: 2021-04-17 16:49:27             🧑  作者: Mango

给定一个由N个整数和一个整数X组成的数组arr [] ,任务是通过多次执行以下操作来找到使所有数组元素大于或等于0所需的最小成本:

  • 将任何数组元素增加1。成本= 1。
  • 将所有数组元素加1。成本=X。

例子:

方法:想法是使用贪婪方法解决问题。请按照以下步骤解决问题:

  • 以升序对数组arr []进行排序。
  • 初始化一个辅助向量,例如list,以存储负数组元素。
  • 初始化一个变量cost = 0,以存储使当前数组元素为0所需的成本并初始化另一个变量min_cost = INT_MAX ,以存储使所有数组元素> = 0的最终最小成本。
  • 遍历数组arr []并尝试通过应用适当的操作来转换列表> = 0中的所有数组元素,并相应地更新min_cost
  • 打印min_cost的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above aproach
 
#include 
using namespace std;
 
// Function to find the minimum
// cost to make all array elements
// greater than or equal to 0
void minCost(int arr[], int N, int X)
{
    // Sort the array in
    // ascending order
    sort(arr, arr + N);
 
    int sum = 0;
 
    // Stores the cost to make
    // current array element >= 0
    int cost = 0;
 
    // Stores the cost to make
    // all array elements >= 0
    int min_cost = INT_MAX;
 
    // Traverse the array and insert all the
    // elements which are < 0
    for (int i = 0; i < N; i++) {
 
        // If current array element
        // is negative
        if (arr[i] < 0) {
 
            // Cost to make all array
            // elements >= 0
            cost = abs(arr[i]) * X
                   + (sum - abs(arr[i]) * i);
            sum += abs(arr[i]);
 
            // Update curr if ans is minimum
            min_cost = min(min_cost, cost);
        }
    }
 
    // Print the minimum cost
    cout << min_cost;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { -1, -3, -2, 4, -1 };
 
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Given value of X
    int X = 2;
 
    // Function call to find minimum
    // cost to make all array elements >= 0
    minCost(arr, N, X);
 
    return 0;
}


Java
// Java program for the above aproach
import java.util.Arrays;
public class GFG
{   
 
  // Function to find the minimum
  // cost to make all array elements
  // greater than or equal to 0
  static void minCost(int arr[], int N, int X)
  {
 
    // Sort the array in
    // ascending order
    Arrays.sort(arr) ;
 
    int sum = 0;
 
    // Stores the cost to make
    // current array element >= 0
    int cost = 0;
 
    int INT_MAX = Integer.MAX_VALUE;
 
    // Stores the cost to make
    // all array elements >= 0
    int min_cost = INT_MAX;
 
    // Traverse the array and insert all the
    // elements which are < 0
    for (int i = 0; i < N; i++) {
 
      // If current array element
      // is negative
      if (arr[i] < 0) {
 
        // Cost to make all array
        // elements >= 0
        cost = Math.abs(arr[i]) * X
          + (sum - Math.abs(arr[i]) * i);
        sum += Math.abs(arr[i]);
 
        // Update curr if ans is minimum
        min_cost = Math.min(min_cost, cost);
      }
    }
 
    // Print the minimum cost
    System.out.print(min_cost);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Given array
    int arr[] = { -1, -3, -2, 4, -1 };
 
    // Size of the array
    int N = arr.length;
 
    // Given value of X
    int X = 2;
 
    // Function call to find minimum
    // cost to make all array elements >= 0
    minCost(arr, N, X);
 
  }
 
}
 
// This code is contributed by AnkThon


Python3
# Python3 program for the above approach
import sys
 
# Function to find the minimum
# cost to make all array of elements
# greater than or equal to 0
def mincost(arr, N, X):
   
    # sort the array in
    # ascending order
    arr.sort()
    sum = 0
     
    # stores the count to make
    # current array element >=0
    cost = 0
     
    # stores the cost to make
    # all array elements >=0
    min_cost = sys.maxsize
     
    # Traverse the array and insert all the
    # elements which are <=0
    for i in range(0, N):
       
        # if current array element
        # is negtive
        if (arr[i] < 0):
           
            # cost to make all array
            # elements >=0
            cost = abs(arr[i]) * x + (sum - abs(arr[i]) * i)
            sum += abs(arr[i])
             
            # update curr if ans is minimum
            min_cost = min(min_cost,cost)
     
    # return minimum cost
    return min_cost
 
# Driver code
arr = [-1, -3, -2, 4, -1]
 
# size of the array
N = len(arr)
 
# Given value of x
x = 2
 
# Function call to find minimum
# cost to make all array elements >=0
print(mincost(arr, N, x))
 
# This code is contributed by Virusbuddah


C#
// C# program for the above approach
using System;
class GFG{
 
// Function to find the minimum
// cost to make all array elements
// greater than or equal to 0
static void minCost(int[] arr, int N, int X)
{
 
    // Sort the array in
    // ascending order
    Array.Sort(arr) ;
     
    int sum = 0;
     
    // Stores the cost to make
    // current array element >= 0
    int cost = 0;
     
    //int INT_MAX = Int32.MaxValue;
     
    // Stores the cost to make
    // all array elements >= 0
    int min_cost = Int32.MaxValue;
     
    // Traverse the array and insert all the
    // elements which are < 0
    for(int i = 0; i < N; i++)
    {
     
        // If current array element
        // is negative
        if (arr[i] < 0)
        {
         
            // Cost to make all array
            // elements >= 0
            cost = Math.Abs(arr[i]) * X + 
            (sum - Math.Abs(arr[i]) * i);
            sum += Math.Abs(arr[i]);
             
            // Update curr if ans is minimum
            min_cost = Math.Min(min_cost, cost);
        }
    }
     
    // Print the minimum cost
    Console.Write(min_cost);
}
 
// Driver Code
static public void Main ()
{
     
    // Given array
    int[] arr = { -1, -3, -2, 4, -1 };
 
    // Size of the array
    int N = arr.Length;
 
    // Given value of X
    int X = 2;
 
    // Function call to find minimum
    // cost to make all array elements >= 0
    minCost(arr, N, X);
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
5

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