给定一个由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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。