📜  使数组递减的最小减法运算数

📅  最后修改于: 2021-04-24 19:34:28             🧑  作者: Mango

您将得到一个由数字arr [0],arr [1],…,arr [N – 1]和一个正整数K组成的序列。在每个运算中,都可以从数组的任何元素中减去K。您需要找到使给定数组减少的最小操作数。
数组arr[0], arr[1], ....., arr[N-1]  称为减少,如果arr[i] >= arr[i+1]  对于每个我: 0 <= i < N-1

方法 :

1.从1到n-1遍历数组的每个元素。 2.检查是否(arr [i]> arr [i-1]),然后查找noOfSubtraction ; noOfSubtraction = (arr[i] - arr[i-1]) / k如果(((arr [i]-arr [i-1])%k == 0),则noOfSubtraction ++修改arr [i] ; arr [i] = arr[i] - ( k * noOfSubtraction )

下面是上述方法的实现:

CPP
// CPP program to make an array decreasing
#include 
using namespace std;
 
// Function to count minimum no of operation
int min_noOf_operation(int arr[], int n, int k)
{
    int noOfSubtraction;
    int res = 0;
    for (int i = 1; i < n; i++) {
        noOfSubtraction = 0;
 
        if (arr[i] > arr[i - 1]) {
 
            // Count how many times we have to subtract.
            noOfSubtraction = (arr[i] - arr[i - 1]) / k;
 
            // Check an additional subtraction is
            // required or not.
            if ((arr[i] - arr[i - 1]) % k != 0)
                noOfSubtraction++;
 
            // Modify the value of arr[i].
            arr[i] = arr[i] - k * noOfSubtraction;
        }
 
        // Count total no of operation/subtraction .
        res = res + noOfSubtraction;
    }
 
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 1, 2, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int k = 5;
    cout << min_noOf_operation(arr, N, k) << endl;
    return 0;
}


Java
// Java program to make an
// array decreasing
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // Function to count minimum no of operation
    public static int min_noOf_operation(int arr[],
                                      int n, int k)
    {
        int noOfSubtraction;
        int res = 0;
         
        for (int i = 1; i < n; i++) {
            noOfSubtraction = 0;
 
            if (arr[i] > arr[i - 1]) {
     
                // Count how many times
                // we have to subtract.
                noOfSubtraction = (arr[i] - arr[i - 1]) / k;
 
                // Check an additional subtraction
                // is required or not.
                if ((arr[i] - arr[i - 1]) % k != 0)
                    noOfSubtraction++;
 
                // Modify the value of arr[i]
                arr[i] = arr[i] - k * noOfSubtraction;
            }
 
            // Count total no of subtraction
            res = res + noOfSubtraction;
        }
 
        return res;
    }
     
    // driver function
    public static void main(String argc[]){
        int arr = { 1, 1, 2, 3 };
        int N = 4;
        int k = 5;
        System.out.println(min_noOf_operation(arr,
                                           N, k));
    }
     
}
 
/* This code is contributed by Sagar Shukla */


Python3
# Python program to make an array decreasing
 
# Function to count minimum no of operation
def min_noOf_operation(arr, n, k):
 
    res = 0
    for i in range(1,n):
        noOfSubtraction = 0
 
        if (arr[i] > arr[i - 1]):
 
            # Count how many times we have to subtract.
            noOfSubtraction = (arr[i] - arr[i - 1]) / k;
 
            # Check an additional subtraction is
            # required or not.
            if ((arr[i] - arr[i - 1]) % k != 0):
                noOfSubtraction+=1
 
            # Modify the value of arr[i].
            arr[i] = arr[i] - k * noOfSubtraction
         
 
        # Count total no of operation/subtraction .
        res = res + noOfSubtraction
     
 
    return int(res)
 
 
# Driver Code
arr = [ 1, 1, 2, 3 ]
N = len(arr)
k = 5
print(min_noOf_operation(arr, N, k))
 
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program to make an
// array decreasing
using System;
 
public class GfG{
     
    // Function to count minimum no of operation
    public static int min_noOf_operation(int []arr,
                                       int n, int k)
    {
        int noOfSubtraction;
        int res = 0;
         
        for (int i = 1; i < n; i++) {
            noOfSubtraction = 0;
 
            if (arr[i] > arr[i - 1]) {
     
                // Count how many times
                // we have to subtract.
                noOfSubtraction = (arr[i] - arr[i - 1]) / k;
 
                // Check an additional subtraction
                // is required or not.
                if ((arr[i] - arr[i - 1]) % k != 0)
                    noOfSubtraction++;
 
                // Modify the value of arr[i]
                arr[i] = arr[i] - k * noOfSubtraction;
            }
 
            // Count total no of subtraction
            res = res + noOfSubtraction;
        }
 
        return res;
    }
     
    // driver function
    public static void Main()
    {
        int []arr = { 1, 1, 2, 3 };
        int N = 4;
        int k = 5;
        Console.WriteLine(min_noOf_operation(arr,
                                        N, k));
    }
     
}
 
// This code is contributed by vt_m


PHP
 $arr[$i - 1])
        {
 
            // Count how many times we
            // have to subtract.
            $noOfSubtraction = ($arr[$i] -
                      $arr[$i - 1]) / $k;
 
            // Check an additional subtraction
            // is required or not.
            if (($arr[$i] - $arr[$i - 1])
                               % $k != 0)
                $noOfSubtraction++;
 
            // Modify the value of arr[i].
            $arr[$i] = $arr[$i] - $k *
                      $noOfSubtraction;
        }
 
        // Count total no of
        // operation/subtraction .
        $res = $res + $noOfSubtraction;
    }
 
    return floor($res);
}
 
    // Driver Code
    $arr = array(1, 1, 2, 3);
    $N = count($arr);
    $k = 5;
    echo min_noOf_operation($arr, $N, $k) ;
 
// This code is contributed by anuj_67.
?>


Javascript


输出 :

3

时间复杂度: O(N)。