给定两个长度为N的数组A []和B [] ,任务是找到可以将数组A转换为数组B的最小操作数,其中每个操作包括将一个整数K加到从L到A的子数组中。 R.
例子:
Input: A[] = {3, 7, 1, 4, 1, 2}, B[] = {3, 7, 3, 6, 3, 2}
Output: 1
Explanation:
In the above given example only one operation is required to convert from A to B: L = 3, R = 5 and K = 2
Array after the following operation:
Index 0: A[0] = 3, B[0] = 3
Index 1: A[1] = 7, B[1] = 7
Index 2: A[2] = 1 + 2 = 3, B[2] = 3
Index 3: A[3] = 4 + 2 = 6, B[3] = 6
Index 4: A[4] = 1 + 2 = 3, B[4] = 3
Index 5: A[5] = 2, B[5] = 2
Input: A[] = {1, 1, 1, 1, 1}, B[] = {1, 2, 1, 3, 1}
Output: 2
Explanation:
In the above given example only one operation is required to convert from A to B –
Operation 1: Add 1 to L = 2 to R = 2
Operation 2: Add 2 to L = 4 to R = 4
方法:想法是计算数组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) 。