📌  相关文章
📜  最小化使数组不增加所需的子数组每个元素的增量计数

📅  最后修改于: 2021-05-17 22:32:07             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到最小数量的操作,这涉及使子数组的所有元素增加1,这使该数组不增加。
例子:

方法:该方法基于这样的事实,即操作只能在子阵列上执行。因此,仅检查连续的元素。步骤如下:

  1. 否则,请初始化一个变量,例如res ,以存储所需的操作计数。
  2. 现在,遍历数组,对于每个元素,检查索引i处的元素是否小于索引(i + 1)处的元素。如果确定为真,则将它们之间的差值添加到res ,因为两个元素都必须相等以使数组不增加。
  3. 否则,请移至下一个元素并重复上述步骤。
  4. 最后,在完成遍历数组之后,输出res的最终值。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the minimum
// number of operations required to
// make the array non-increasing
int getMinOps(int arr[], int n)
{
   
    // Stores the count of
    // required operations
    int res = 0;
 
    for (int i = 0; i < n - 1; i++)
    {
 
        // If arr[i] > arr[i+1],
        // no increments required.
        // Otherwise, add their
        // difference to the answer
        res += max(arr[i + 1] - arr[i], 0);
    }
 
    // Return the result res
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = {1, 3, 4, 1, 2};
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << getMinOps(arr, N);
}
 
// This code is contributed by shikhasingrajput


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to find the minimum
    // number of operations required to
    // make the array non-increasing
    public static int getMinOps(int[] arr)
    {
        // Stores the count of
        // required operations
        int res = 0;
 
        for (int i = 0;
             i < arr.length - 1; i++)
        {
 
            // If arr[i] > arr[i+1],
            // no increments required.
            // Otherwise, add their
            // difference to the answer
            res += Math.max(arr[i + 1]
                            - arr[i],
                            0);
        }
 
        // Return the result res
        return res;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[] arr = { 1, 3, 4, 1, 2 };
        System.out.println(getMinOps(arr));
    }
}


Python3
# Python3 Program to implement
# the above approach
 
# Function to find the minimum
# number of operations required to
# make the array non-increasing
def getMinOps(arr):
 
    # Stores the count of
    # required operations
    res = 0
 
    for i in range(len(arr) - 1):
       
        # If arr[i] > arr[i+1],
        # no increments required.
        # Otherwise, add their
        # difference to the answer
        res += max(arr[i + 1] - arr[i], 0)
 
    # Return the result res
    return res
 
# Driver Code
 
# Given array
arr = [ 1, 3, 4, 1, 2 ]
 
# Function call
print(getMinOps(arr))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum
// number of operations required to
// make the array non-increasing
public static int getMinOps(int[] arr)
{
     
    // Stores the count of
    // required operations
    int res = 0;
 
    for(int i = 0; i < arr.Length - 1; i++)
    {
         
        // If arr[i] > arr[i+1],
        // no increments required.
        // Otherwise, add their
        // difference to the answer
        res += Math.Max(arr[i + 1] -
                        arr[i], 0);
    }
 
    // Return the result res
    return res;
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 1, 3, 4, 1, 2 };
     
    Console.WriteLine(getMinOps(arr));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4

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