📌  相关文章
📜  形成给定数组所需的 K 大小子数组的最小增量计数

📅  最后修改于: 2021-09-22 10:23:07             🧑  作者: Mango

给定一个数组arr[]和一个整数K ,任务是找到更改大小为N的数组B所需的最小操作次数,该数组B包含全零,使得 B 的每个元素都大于或等于 arr。即,arr[i] >= B[i]。在任何操作中,您都可以选择一个大小为 K 的 B 子数组,并将该子数组的所有元素加 1。

例子:

方法:这个想法是每当有 B[i] 小于 arr[i] 时增加大小为 K 的子数组,并且在每一步将此类操作的计数增加 1。要增加大小为 K 的子数组,请在 O(1) 中使用差异数组进行范围查询更新。

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum number of operations
// required to change an array of
// all zeros such that every element
// is greater than the given array
#include 
using namespace std;
 
// Function to find the minimum
// number of operations required
// to change all the array of zeros
// such that every element is greater
// than the given array
int find_minimum_operations(int n, int b[],
                            int k)
{
     
    // Declaring the difference
    // array of size N
    int d[n + 1] = {0};
 
    // Number of operations
    int operations = 0, need;
 
    for(int i = 0; i < n; i++)
    {
         
        // First update the D[i] value
        // with the previous value
        if (i > 0)
        {
            d[i] += d[i - 1];
        }
         
        // The index i has to be incremented
        if (b[i] > d[i])
        {
             
            // We have to perform
            // (b[i]-d[i]) operations more
            operations += b[i] - d[i];
 
            need = b[i] - d[i];
 
            // Increment the range
            // i to i + k by need
            d[i] += need;
 
            // Check if i + k is valid index
            if(i + k <= n)
            {
                d[i + k]-= need;
            }
        }
    }
    cout << operations << endl;
}
 
// Driver Code
int main()
{
    int n = 5;
    int b[] = { 1, 2, 3, 4, 5 };
    int k = 2;
     
    // Function Call
    find_minimum_operations(n, b, k);
     
    return 0;
}
 
// This code is contributed by shubhamsingh10


Java
// Java implementation to find the
// minimum number of operations
// required to change an array of
// all zeros such that every element
// is greater than the given array
class GFG{
 
// Function to find the minimum
// number of operations required
// to change all the array of zeros
// such that every element is greater
// than the given array
static void find_minimum_operations(int n, int b[],
                                    int k)
{
     
    // Declaring the difference
    // array of size N
    int d[] = new int[n + 1];
 
    // Number of operations
    int i, operations = 0, need;
 
    for(i = 0; i < n; i++)
    {
         
        // First update the D[i] value
        // with the previous value
        if (i > 0)
        {
            d[i] += d[i - 1];
        }
         
        // The index i has to be incremented
        if (b[i] > d[i])
        {
             
            // We have to perform
            // (b[i]-d[i]) operations more
            operations += b[i] - d[i];
 
            need = b[i] - d[i];
 
            // Increment the range
            // i to i + k by need
            d[i] += need;
 
            // Check if i + k is valid index
            if(i + k <= n)
            {
                d[i + k]-= need;
            }
        }
    }
    System.out.println(operations);
}
 
// Driver Code
public static void main (String []args)
{
    int n = 5;
    int b[] = { 1, 2, 3, 4, 5 };
    int k = 2;
     
    // Function Call
    find_minimum_operations(n, b, k);
}
}
 
// This code is contributed by chitranayal


Python3
# Python3 implementation to find the
# minimum number of operations required
# to change an array of all zeros
# such that every element is greater than
# the given array
 
# Function to find the minimum
# number of operations required
# to change all the array of zeros
# such that every element is greater
# than the given array
def find_minimum_operations(n, b, k):
 
    # Declaring the difference
    # array of size N
    d =[0 for i in range(n + 1)]
 
    # Number of operations
    operations = 0
 
    for i in range(n):
 
        # First update the D[i] value with
        # the previous value
        d[i]+= d[i-1]
 
        # The index i has to be incremented
        if b[i]>d[i]:
 
            # We have to perform
            # (b[i]-d[i]) operations more
            operations+=(b[i]-d[i])
 
            need =(b[i]-d[i])
 
            # Increment the range
            # i to i + k by need
            d[i]+= need
 
            # Check if i + k is valid index
            if i + k<= n:
                d[i + k]-= need
 
    return operations
 
# Driver Code
if __name__ == "__main__":
    n = 5
    b =[1, 2, 3, 4, 5]
    k = 2
     
    # Function Call
    print(find_minimum_operations(n, b, k))


C#
// C# implementation to find the
// minimum number of operations
// required to change an array of
// all zeros such that every element
// is greater than the given array
using System;
class GFG{
  
// Function to find the minimum
// number of operations required
// to change all the array of zeros
// such that every element is greater
// than the given array
static void find_minimum_operations(int n, int[] b,
                                    int k)
{
      
    // Declaring the difference
    // array of size N
    int[] d = new int[n + 1];
  
    // Number of operations
    int i, operations = 0, need;
  
    for(i = 0; i < n; i++)
    {
          
        // First update the D[i] value
        // with the previous value
        if (i > 0)
        {
            d[i] += d[i - 1];
        }
          
        // The index i has to be incremented
        if (b[i] > d[i])
        {
              
            // We have to perform
            // (b[i]-d[i]) operations more
            operations += b[i] - d[i];
  
            need = b[i] - d[i];
  
            // Increment the range
            // i to i + k by need
            d[i] += need;
  
            // Check if i + k is valid index
            if(i + k <= n)
            {
                d[i + k]-= need;
            }
        }
    }
    Console.Write(operations);
}
  
// Driver Code
public static void Main (string []args)
{
    int n = 5;
    int[] b = { 1, 2, 3, 4, 5 };
    int k = 2;
      
    // Function Call
    find_minimum_operations(n, b, k);
}
}
  
// This code is contributed by rock_cool


Javascript


输出:
9

时间复杂度:O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程