📌  相关文章
📜  使一个数组的所有元素乘以另一个数组所需的最小前缀增量

📅  最后修改于: 2021-05-14 00:13:47             🧑  作者: Mango

给定两个阵列A []B []的大小为N,任务是找到操作的最小计数需要使A [I] B [I]的倍数由递增1前缀的子阵列。

例子:

方法:可以使用贪婪技术解决问题。为了最大程度地减少运算次数,我们的想法是找到A [i]的最接近的最小较大或相等元素,元素是B [i]的倍数:

  1. 从头到尾遍历数组A []
  2. 找到B []对应元素的最接近倍数之间最小差K。
  3. 由于K等于i元素运算次数,因此0索引到第(i-1)索引的所有元素值将增加K。
  4. 现在,维护一个变量进位,该进位将存储累积增量,因此,如果i元素增加K次,则将K添加到进位
  5. 进位变量的值将用于查找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.


输出:
22

时间复杂度: O(N)
辅助空间: O(1)