给定一个由N个整数组成的数组arr [] ,任务是找到最小数量的操作,这涉及使子数组的所有元素增加1,这使该数组不增加。
例子:
Input: arr[] = {1, 3, 4, 1, 2}
Output: 4
Explanation:
In operation 1: Choose the subarray {1} and increase its value by 1. Now arr[] = {2, 3, 4, 1, 2}
In operation 2: Choose the subarray {2} and increase its value by 1. Now arr[] = {3, 3, 4, 1, 2}
In operation 3: Choose the subarray {3, 3} and increase its value by 1. Now arr[] = {4, 4, 4, 1, 2}
In operation 4: Choose the subarray {1} and increase its value by 1. Now arr[] = {4, 4, 4, 2, 2}
Thus, it took 4 operations to make the array non-increasing.
Input: arr[] = {10, 9, 8, 7, 7}
Output: 0
Explanation:
The array is already non-increasing
方法:该方法基于这样的事实,即操作只能在子阵列上执行。因此,仅检查连续的元素。步骤如下:
- 否则,请初始化一个变量,例如res ,以存储所需的操作计数。
- 现在,遍历数组,对于每个元素,检查索引i处的元素是否小于索引(i + 1)处的元素。如果确定为真,则将它们之间的差值添加到res ,因为两个元素都必须相等以使数组不增加。
- 否则,请移至下一个元素并重复上述步骤。
- 最后,在完成遍历数组之后,输出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)