📌  相关文章
📜  最小化将所有数组元素减少到 0 所需的子数组增量/减量

📅  最后修改于: 2021-09-03 03:07:22             🧑  作者: Mango

给定一个数组arr[],选择任何子数组并对子数组的每个元素应用以下任一操作:

  • 加一
  • 减一

任务是打印将所有数组元素减少到 0 所需的上述递增/递减操作的最小次数。

例子:

方法:求解问题,遍历数组,找到最小负数和最大正数。它们的绝对值之和是所需的最少操作次数。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to count the minimum
// number of operations required
int minOperation(int arr[], int N)
{
  int minOp = INT_MIN;
  int minNeg = 0, maxPos = 0;
 
  // Traverse the array
  for (int i = 0; i < N; i++)
  {
    // If array element
    // is negative
    if (arr[i] < 0)
    {
      if (arr[i] < minNeg)
 
        // Update minimum negative
        minNeg = arr[i];
    }
    else
    {
      if (arr[i] > maxPos)
 
        // Update maximum positive
        maxPos = arr[i];
    }
  }
 
  // Return minOp
  return abs(minNeg) + maxPos;
}
 
// Driver Code
int main()
{
  int arr[] = {1, 3, 4, 1};
  int N = sizeof(arr) / sizeof(arr[0]);
  cout << minOperation(arr, N);
}
 
//This code is contributed by Rajput-Ji


Java
// Java program of the
// above approach
 
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to count the minimum
    // number of operations required
    static int minOperation(int[] arr)
    {
 
        int minOp = Integer.MIN_VALUE;
        int minNeg = 0, maxPos = 0;
 
        // Traverse the array
        for (int i = 0; i < arr.length; i++) {
 
            // If array element
            // is negative
            if (arr[i] < 0) {
                if (arr[i] < minNeg)
 
                    // Update minimum negative
                    minNeg = arr[i];
            }
            else {
                if (arr[i] > maxPos)
 
                    // Update maximum positive
                    maxPos = arr[i];
            }
        }
 
        // Return minOp
        return Math.abs(minNeg) + maxPos;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 3, 4, 1 };
        System.out.println(minOperation(arr));
    }
}


Python3
# Python3 program of the
# above approach
 
import sys
 
# Function to count the minimum
# number of operations required
def minOperation(arr):
     
    minOp = sys.maxsize
    minNeg = 0
    maxPos = 0
     
    # Traverse the array
    for i in range(len(arr)):
       
        # If array element
        # is negative
        if(arr[i] < 0):
             
            if (arr[i] < minNeg):
                     
                # Update minimum negative
                minNeg = arr[i]
        else:
            if arr[i] > maxPos:
                 
                # Update maximum position
                maxPos = arr[i]
     
    # Return minOp
    return abs(minNeg) + maxPos
 
# Driver code 
if __name__=="__main__":   
    arr=[1, 3, 4, 1]
    print(minOperation(arr))
 
# This code is contributed by Rutvik_56


C#
// C# program of the
// above approach
using System;
class GFG{
 
// Function to count the minimum
// number of operations required
static int minOperation(int[] arr)
{
 
  int minOp = int.MinValue;
  int minNeg = 0, maxPos = 0;
 
  // Traverse the array
  for (int i = 0; i < arr.Length; i++)
  {
    // If array element
    // is negative
    if (arr[i] < 0)
    {
      if (arr[i] < minNeg)
 
        // Update minimum negative
        minNeg = arr[i];
    }
    else
    {
      if (arr[i] > maxPos)
 
        // Update maximum positive
        maxPos = arr[i];
    }
  }
 
  // Return minOp
  return Math.Abs(minNeg) + maxPos;
}
 
// Driver Code
public static void Main(String[] args)
{
  int []arr = {1, 3, 4, 1};
  Console.WriteLine(minOperation(arr));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
4

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live