给定一个由N 个非负整数组成的数组arr[] ,任务是找到需要减少 1 以使所有数组元素等于 0 的最小子数组数。
例子:
Input: arr[] = {1, 2, 3, 2, 1}
Output: 3
Explanation:
Operation 1: {1, 2, 3, 2, 1} -> {0, 1, 2, 1, 0}
Operation 2: {0, 1, 2, 1, 0} -> {0, 0, 1, 0, 0}
Operation 3: {0, 0, 1, 0, 0} -> {0, 0, 0, 0, 0}
Input: arr[] = {5, 4, 3, 4, 4}
Output: 6
Explanation:
{5, 4, 3, 4, 4} -> {4, 3, 2, 3, 3} -> {3, 2, 1, 2, 2} -> {2, 1, 0, 1, 1} -> {2, 1, 0, 0, 0} -> {1, 0, 0, 0, 0} -> {0, 0, 0, 0, 0}
方法:
这可以通过从索引0遍历给定数组,找到直到索引i的答案来最佳地完成,其中0 ≤ i < N 。如果arr[i] ≥ arr[i+1] ,则第(i + 1)个元素可以包含在第i个元素的每个子数组操作中,因此不需要额外的操作。如果arr[i] < arr[i + 1] ,则第(i + 1)个元素可以包含在第i个元素的每个子数组操作中,并且在所有操作之后, arr[i+1]变为arr[i+1] -arr[i] 。因此,我们需要arr[i+1]-arr[i]额外的操作来将其归零。
请按照以下步骤解决问题:
- 添加第一个元素arr[0]来回答,因为我们至少需要arr[0]来使给定的数组0 。
- 遍历索引[1, N-1]并且对于每个元素,检查它是否大于前一个元素。如果发现是真的,请将它们的差异添加到答案中。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to count the minimum
// number of subarrays that are
// required to be decremented by 1
int min_operations(vector& A)
{
// Base Case
if (A.size() == 0)
return 0;
// Initialize ans to first element
int ans = A[0];
for (int i = 1; i < A.size(); i++) {
// For A[i] > A[i-1], operation
// (A[i] - A[i - 1]) is required
ans += max(A[i] - A[i - 1], 0);
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
vector A{ 1, 2, 3, 2, 1 };
cout << min_operations(A) << "\n";
return 0;
}
Java
// Java Program to implement
// the above approach
import java.io.*;
class GFG {
// Function to count the minimum
// number of subarrays that are
// required to be decremented by 1
static int min_operations(int A[], int n)
{
// Base Case
if (n == 0)
return 0;
// Initializing ans to first element
int ans = A[0];
for (int i = 1; i < n; i++) {
// For A[i] > A[i-1], operation
// (A[i] - A[i - 1]) is required
if (A[i] > A[i - 1]) {
ans += A[i] - A[i - 1];
}
}
// Return the count
return ans;
}
// Driver Code
public static void main(String[] args)
{
int n = 5;
int A[] = { 1, 2, 3, 2, 1 };
System.out.println(min_operations(A, n));
}
}
Python
# Python Program to implement
# the above approach
# Function to count the minimum
# number of subarrays that are
# required to be decremented by 1
def min_operations(A):
# Base case
if len(A) == 0:
return 0
# Initializing ans to first element
ans = A[0]
for i in range(1, len(A)):
if A[i] > A[i-1]:
ans += A[i]-A[i-1]
return ans
# Driver Code
A = [1, 2, 3, 2, 1]
print(min_operations(A))
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count the minimum
// number of subarrays that are
// required to be decremented by 1
static int min_operations(int[] A, int n)
{
// Base Case
if (n == 0)
return 0;
// Initializing ans to first element
int ans = A[0];
for(int i = 1; i < n; i++)
{
// For A[i] > A[i-1], operation
// (A[i] - A[i - 1]) is required
if (A[i] > A[i - 1])
{
ans += A[i] - A[i - 1];
}
}
// Return the count
return ans;
}
// Driver Code
public static void Main()
{
int n = 5;
int[] A = { 1, 2, 3, 2, 1 };
Console.WriteLine(min_operations(A, n));
}
}
// This code is contributed by bolliranadheer
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live