📌  相关文章
📜  构造给定数组的后缀递增/递减操作计数

📅  最后修改于: 2022-05-13 01:57:05.107000             🧑  作者: Mango

构造给定数组的后缀递增/递减操作计数

给定一个非负整数数组。我们需要从一个全为零的数组构造给定的数组。我们可以进行以下操作。

  • 选择说 i 的任何索引并将所有元素加 1 或从索引 i 到最后一个索引的所有元素中减去 1。我们基本上将后缀增加/减少 1。

例子 :

Input : brr[] = {1, 2, 3, 4, 5}
Output : 5
Here, we can successively choose indices 1, 2, 
3, 4, and 5, and add 1 to corresponding suffixes.

Input : brr[] = {1, 2, 2, 1}
Output : 3
Here, we choose indices 1 and 2 and adds 1 to 
corresponding suffixes, then we choose index 4 
and subtract 1.

brr[]为数组, arr[]为当前数组(最初为 0)。
方法很简单:

  • 为了使第一个元素相等,我们必须使 |brr[1]|操作。一旦完成,arr[2], arr[3], arr[4], ... arr[n] = brr[1]。
  • 为了使第二个元素相等,我们必须使 |brr[2] – brr[1]|操作。一旦完成,arr[3], arr[4], arr[5], ... arr[n] = brr[2]。

一般来说,要使 arr[i] = brr[i] 我们需要使 |brr[i] – b[i – 1]|操作。所以总的来说,我们必须制作 |b[1]| + |b[2] – b[1]| + |b[3] – b[2]| + … + |b[n] – b[n – 1]|操作。
以下是上述方法的 CPP 和Java实现:

C++
// CPP program to find minimum number of steps
// to make the array equal to the given array.
#include 
using namespace std;
 
// function to calculate min_Steps
int minSteps(int arr[], int n)
{
    int min_Steps = 0;
    for (int i = 0; i < n; i++) {
        if (i > 0)
            min_Steps += abs(arr[i] - arr[i - 1]);
         
        // first element of arr.
        else
            min_Steps += abs(arr[i]);
    }
    return min_Steps;
}
 
// driver function
int main()
{
    int arr[] = { 1, 2, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << minSteps(arr, n) << endl;
}


Java
// Java program to find minimum number of steps
// to make the array equal to the given array.
import java.util.*;
import java.lang.*;
 
public class GfG {
    // function to calculate min_Steps
    public static int minSteps(int arr[], int n)
    {
        int min_Steps = 0;
        for (int i = 0; i < n; i++) {
            if (i > 0)
                min_Steps +=
                    Math.abs(arr[i] - arr[i - 1]);
             
            // first element of arr.
            else
                min_Steps += Math.abs(arr[i]);
        }
        return min_Steps;
    }
 
    // driver function
    public static void main(String argc[])
    {
        int[] arr = new int[] { 1, 2, 2, 1 };
        int n = 4;
        System.out.println(minSteps(arr, n));
    }
}


Python3
# Python 3 program to find minimum number
# of steps to make the array equal to the
# given array.
 
# function to calculate min_Steps
def minSteps(arr, n):
    min_Steps = 0
    for i in range(n):
        if (i > 0):
            min_Steps += abs(arr[i] -
                             arr[i - 1])
         
        # first element of arr.
        else:
            min_Steps += abs(arr[i])
    return min_Steps
 
# Driver Code
if __name__ == '__main__':
    arr = [ 1, 2, 2, 1 ]
    n = len(arr)
    print(minSteps(arr, n))
 
# This code is contributed
# by PrinciRaj19992


C#
// C# program to find minimum number of steps
// to make the array equal to the given array.
using System;
 
public class GfG {
     
    // function to calculate min_Steps
    public static int minSteps(int[] arr, int n)
    {
        int min_Steps = 0;
        for (int i = 0; i < n; i++) {
            if (i > 0)
                min_Steps += Math.Abs(arr[i] - arr[i - 1]);
 
            // first element of arr.
            else
                min_Steps += Math.Abs(arr[i]);
        }
        return min_Steps;
    }
 
    // driver function
    public static void Main()
    {
        int[] arr = new int[] { 1, 2, 2, 1 };
        int n = 4;
        Console.WriteLine(minSteps(arr, n));
    }
}
 
// This code is contributed by vt_m


PHP
 0)
            $min_Steps += abs($arr[$i] -
                              $arr[$i - 1]);
         
        // first element of arr.
        else
            $min_Steps += abs($arr[$i]);
    }
    return $min_Steps;
}
 
// Driver Code
$arr = array( 1, 2, 2, 1 );
$n = sizeof($arr) ;
 
echo minSteps($arr, $n),"\n";
 
// This code is contributed by ajit
?>


Javascript


输出 :

3

时间复杂度= O(n)。