给定两个阵列A []和B []的大小为N,任务是找到操作的最小计数需要使A [I] B [I]的倍数由递增1前缀的子阵列。
例子:
Input: A[ ] = {3, 2, 9}, B[ ] = {5, 7, 4}, N = 3
Output: 7
Explanation:
Incrementing {A[0]} twice modifies A[] to {5, 2, 9}
Incrementing all the elements of subarray {A[0], A[1]} twice modifies A[] to {7, 4, 9}
Incrementing all the elements of the subarray {A[0], A[1], A[2]} thrice modifies A[] to {10, 7, 12}
Therefore, total operations required = 2 + 2 + 3 = 7.
Input: A[ ] = {3, 4, 5, 2, 5, 5, 9}, B[ ] = {1, 1, 9, 6, 3, 8, 7}, N = 7
Output: 22
方法:该问题可以使用贪心技术解决。为了最小化操作次数,想法是找到A[i]的最接近的最小大于或等于元素,它是B[i]的倍数:
- 从头到尾遍历数组A[] 。
- 找到B[]的对应元素的最近倍数之间的最小差异K 。
- 由于K等于第i个元素的操作次数,因此从第0个索引到第(i-1)个索引的所有元素的值将增加K。
- 现在维护一个将存储累积增量的变量进位,因此如果第i个元素增加K次,则将K添加到进位。
- 进位变量的值将用于查找A[] 的第(i-1)个元素的新值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum count of operations
// required to make A[i] multiple of B[i] by
// incrementing prefix subarray
int MinimumMoves(int A[], int B[], int N)
{
// Stores minimum count of operations
// required to make A[i] multiple of B[i]
// by incrementing prefix subarray
int totalOperations = 0;
// Stores the carry
int carry = 0;
// Stores minimum difference of
// correspoinding element in
// prefix subarray
int K = 0;
// Traverse the array
for (int i = N - 1; i >= 0; i--) {
// Stores the closest greater or equal number
// to A[i] which is a multiple of B[i]
int nearestMultiple = ceil((double)(A[i] + carry)
/ (double)(B[i]))
* B[i];
// Stores minimum difference
K = nearestMultiple - (A[i] + carry);
// Update totalOperations
totalOperations += K;
// Update carry
carry += K;
}
return totalOperations;
}
// Driver Code
int main()
{
// Input arrays A[] and B[]
int A[] = { 3, 4, 5, 2, 5, 5, 9 };
int B[] = { 1, 1, 9, 6, 3, 8, 7 };
// Length of arrays
int N = sizeof(A) / sizeof(A[0]);
cout << MinimumMoves(A, B, N) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find minimum count of operations
// required to make A[i] multiple of B[i] by
// incrementing prefix subarray
static int MinimumMoves(int A[], int B[], int N)
{
// Stores minimum count of operations
// required to make A[i] multiple of B[i]
// by incrementing prefix subarray
int totalOperations = 0;
// Stores the carry
int carry = 0;
// Stores minimum difference of
// correspoinding element in
// prefix subarray
int K = 0;
// Traverse the array
for (int i = N - 1; i >= 0; i--)
{
// Stores the closest greater or equal number
// to A[i] which is a multiple of B[i]
int nearestMultiple = (int) (Math.ceil((double)(A[i] + carry)
/ (double)(B[i]))
* B[i]);
// Stores minimum difference
K = nearestMultiple - (A[i] + carry);
// Update totalOperations
totalOperations += K;
// Update carry
carry += K;
}
return totalOperations;
}
// Driver Code
public static void main(String[] args)
{
// Input arrays A[] and B[]
int A[] = { 3, 4, 5, 2, 5, 5, 9 };
int B[] = { 1, 1, 9, 6, 3, 8, 7 };
// Length of arrays
int N = A.length;
System.out.print(MinimumMoves(A, B, N) +"\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
from math import ceil,floor
# Function to find minimum count of operations
# required to make A[i] multiple of B[i] by
# incrementing prefix subarray
def MinimumMoves(A, B, N):
# Stores minimum count of operations
# required to make A[i] multiple of B[i]
# by incrementing prefix subarray
totalOperations = 0
# Stores the carry
carry = 0
# Stores minimum difference of
# correspoinding element in
# prefix subarray
K = 0
# Traverse the array
for i in range(N - 1, -1, -1):
# Stores the closest greater or equal number
# to A[i] which is a multiple of B[i]
nearestMultiple = ceil((A[i] + carry)/ B[i])* B[i]
# Stores minimum difference
K = nearestMultiple - (A[i] + carry)
# Update totalOperations
totalOperations += K
# Update carry
carry += K
return totalOperations
# Driver Code
if __name__ == '__main__':
# Input arrays A[] and B[]
A = [3, 4, 5, 2, 5, 5, 9]
B = [1, 1, 9, 6, 3, 8, 7]
# Length of arrays
N = len(A)
print (MinimumMoves(A, B, N))
# This code is contributed by mohit kumar 29.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find minimum count of operations
// required to make A[i] multiple of B[i] by
// incrementing prefix subarray
static int MinimumMoves(int[] A, int[] B, int N)
{
// Stores minimum count of operations
// required to make A[i] multiple of B[i]
// by incrementing prefix subarray
int totalOperations = 0;
// Stores the carry
int carry = 0;
// Stores minimum difference of
// correspoinding element in
// prefix subarray
int K = 0;
// Traverse the array
for (int i = N - 1; i >= 0; i--)
{
// Stores the closest greater or equal number
// to A[i] which is a multiple of B[i]
int nearestMultiple = (int) (Math.Ceiling((double)(A[i] + carry)
/ (double)(B[i]))
* B[i]);
// Stores minimum difference
K = nearestMultiple - (A[i] + carry);
// Update totalOperations
totalOperations += K;
// Update carry
carry += K;
}
return totalOperations;
}
// Driver Code
public static void Main(string[] args)
{
// Input arrays A[] and B[]
int[] A = { 3, 4, 5, 2, 5, 5, 9 };
int[] B = { 1, 1, 9, 6, 3, 8, 7 };
// Length of arrays
int N = A.Length;
Console.Write(MinimumMoves(A, B, N) +"\n");
}
}
// This code is contributed by sanjoy_62.
Javascript
22
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。