给定一个数组arr[],选择任何子数组并对子数组的每个元素应用以下任一操作:
- 加一
- 减一
任务是打印将所有数组元素减少到 0 所需的上述递增/递减操作的最小次数。
例子:
Input: arr[] = {1, 3, 4, 1}
Output: 4
Explanation:
Optimal steps to reduce all array elements to 0 are as follows:
Step 1: Select subarray [1, 3, 4, 1] and convert it to [0, 2, 3, 0], by decrementing each element by 1
Array modifies to {0, 2, 3, 0}
Step 2: Select subarray [2, 3] and convert it to [1, 2], by decrementing each element by 1
Array modifies to {0, 1, 2, 0}
Step 3: Select subarray [1, 2] and convert it to [0, 1], by decrementing each element by 1
Array modifies to {0, 0, 1, 0}
Step 4: Select subarray [1] convert it to [0]
Array modifies to {0, 0, 0, 0}
Therefore, minimum number of steps required is 4.
Input: arr[] = {-2, 0, -3, 1, 2}
Output: 5
Explanation:
Optimal steps to reduce all array elements to 0 are as follows:
Step 1: Select subarray [-2, 0, -3] and convert it to [-1, 0, -2], by incrementing each element by 1 except 0.
Array modifies to {-1, 0, -2, 1, 2}
Step 2: Select subarray [-1, 0, -2] and convert it to [0, 0, -1], by incrementing each element by 1 except 0.
Array modifies to {0, 0, -1, 1, 2}
Step 3: Select subarray [-1] convert it to [0]
Array modifies to {0, 0, 0, 1, 2}
Step 4: Select subarray [1, 2] and convert it to [0, 1], by decrementing each element by 1
Array modifies to {0, 0, 0, 0, 1}
Step 5: Select [1] convert it to [0]
Array modifies to {0, 0, 0, 0, 0}
Therefore, minimum number of steps required is 5
方法:求解问题,遍历数组,找到最小负数和最大正数。它们的绝对值之和是所需的最少操作次数。
下面是上述方法的实现:
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)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。