📌  相关文章
📜  通过将整数添加到子数组中将数组 A 转换为数组 B 的最少操作次数

📅  最后修改于: 2021-09-07 02:26:26             🧑  作者: Mango

给定两个长度为N 的数组A[]B[] ,任务是找到可以将数组A转换为数组B的最少操作次数,其中每个操作包括将整数 K 添加到从 L 到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


Javascript


性能分析:

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live