📜  通过将整数添加到子数组来将数组A转换为数组B的最小操作数

📅  最后修改于: 2021-05-05 02:24:25             🧑  作者: Mango

给定两个长度为N的数组A []B [] ,任务是找到可以将数组A转换为数组B的最小操作数,其中每个操作包括将一个整数K加到从L到A的子数组中。 R.

例子:

方法:想法是计算数组A中与数组B中相应元素具有相等差的连续元素。

  • 从数组A和B中找到相应元素的差异:
    Difference = A[i] - B[i]
    
  • 如果相应元素的差等于0,则继续检查下一个索引。
  • 否则,增加索引,直到连续元素之间的差不等于连续元素之前的差
  • 将计数加1,如果所有索引都被迭代,则它们具有相同的差异。
  • 最后,将计数返回为最小操作数。

下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum number of operations in
// which the array A can be converted
// to another array B
  
#include 
using namespace std;
  
// Function to find the minimum
// number of operations in which
// array A can be converted to array B
void checkArray(int a[], int b[], int n)
{
    int operations = 0;
    int i = 0;
      
    // Loop to iterate over the array
    while (i < n) {
          
        // if both elements are equal
        // then move to next element
        if (a[i] - b[i] == 0) {
            i++;
            continue;
        }
  
        // Calculate the difference
        // between two elements
        int diff = a[i] - b[i];
        i++;
  
        // loop while the next pair of
        // elements have same difference
        while (i < n &&
           a[i] - b[i] == diff) {
            i++;
        }
  
        // Increase the number of
        // operations by 1
        operations++;
    }
  
    // Print the number of
    // operations required
    cout << operations << "\n";
}
  
// Driver Code
int main()
{
    int a[] = { 3, 7, 1, 4, 1, 2 };
    int b[] = { 3, 7, 3, 6, 3, 2 };
    int size = sizeof(a) / sizeof(a[0]);
  
    checkArray(a, b, size);
  
    return 0;
}


Java
// Java implementation to find the
// minimum number of operations in
// which the array A can be converted
// to another array B
class GFG {
  
    // Function to find the minimum
    // number of operations in which
    // array A can be converted to array B
    static void checkArray(int a[], int b[], int n)
    {
        int operations = 0;
        int i = 0;
          
        // Loop to iterate over the array
        while (i < n) {
              
            // if both elements are equal
            // then move to next element
            if (a[i] - b[i] == 0) {
                i++;
                continue;
            }
      
            // Calculate the difference
            // between two elements
            int diff = a[i] - b[i];
            i++;
      
            // loop while the next pair of
            // elements have same difference
            while (i < n &&
               a[i] - b[i] == diff) {
                i++;
            }
      
            // Increase the number of
            // operations by 1
            operations++;
        }
      
        // Print the number of
        // operations required
        System.out.println(operations);
    }
      
    // Driver Code
    public static void main (String[] args)
    {
        int a[] = { 3, 7, 1, 4, 1, 2 };
        int b[] = { 3, 7, 3, 6, 3, 2 };
        int size = a.length;
      
        checkArray(a, b, size);
    }
}
  
// This code is contributed by AnkitRai01


C#
// C# implementation to find the
// minimum number of operations in
// which the array A can be converted
// to another array B
using System;
  
class GFG {
  
    // Function to find the minimum
    // number of operations in which
    // array A can be converted to array B
    static void checkArray(int []a, int []b, int n)
    {
        int operations = 0;
        int i = 0;
          
        // Loop to iterate over the array
        while (i < n) {
              
            // if both elements are equal
            // then move to next element
            if (a[i] - b[i] == 0) {
                i++;
                continue;
            }
      
            // Calculate the difference
            // between two elements
            int diff = a[i] - b[i];
            i++;
      
            // loop while the next pair of
            // elements have same difference
            while (i < n &&
               a[i] - b[i] == diff) {
                i++;
            }
      
            // Increase the number of
            // operations by 1
            operations++;
        }
      
        // Print the number of
        // operations required
        Console.WriteLine(operations);
    }
      
    // Driver Code
    public static void Main (string[] args)
    {
        int []a = { 3, 7, 1, 4, 1, 2 };
        int []b = { 3, 7, 3, 6, 3, 2 };
        int size = a.Length;
      
        checkArray(a, b, size);
    }
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation to find the
# minimum number of operations in
# which the array A can be converted
# to another array B
  
# Function to find the minimum
# number of operations in which
# array A can be converted to array B
def checkArray(a, b, n) :
  
    operations = 0;
    i = 0;
      
    # Loop to iterate over the array
    while (i < n) :
          
        # if both elements are equal
        # then move to next element
        if (a[i] - b[i] == 0) :
            i += 1;
            continue;
  
        # Calculate the difference
        # between two elements
        diff = a[i] - b[i];
        i += 1;
  
        # loop while the next pair of
        # elements have same difference
        while (i < n and a[i] - b[i] == diff) :
            i += 1;
  
        # Increase the number of
        # operations by 1
        operations += 1;
      
    # Print the number of
    # operations required
    print(operations);
  
# Driver Code
if __name__ == "__main__" :
  
    a = [ 3, 7, 1, 4, 1, 2 ];
    b = [ 3, 7, 3, 6, 3, 2 ];
    size = len(a);
  
    checkArray(a, b, size);
  
# This code is contributed by AnkitRai01


性能分析:

  • 时间复杂度:与上述方法一样,在最坏的情况下只有一个循环占用O(N)时间。因此,时间复杂度将为O(N)
  • 辅助空间复杂度:与上述方法一样,没有使用额外的空间。因此,辅助空间复杂度将为O(1)