📌  相关文章
📜  通过反转两个数组之一的子数组来最小化两个数组的相同索引元素的乘积之和

📅  最后修改于: 2021-09-06 05:12:22             🧑  作者: Mango

给定两个仅由正整数组成的等长数组A[]B[] ,任务是反转第一个数组的任何子数组,使得两个数组的相同索引元素的乘积之和,即(A [i] * B[i])是最小值。

例子:

方法:给定的问题可以通过使用两个指针技术来解决。请按照以下步骤解决问题:

  • 声明一个变量,比如total ,用于存储两个数组的初始乘积。
  • 声明一个变量,例如min ,以存储所需的答案。
  • 声明两个变量,比如firstsecond ,以存储需要反转以最小化乘积的子数组的开始和结束索引。
  • 通过以下操作计算奇数长度子数组可能的最小乘积:
    • 使用变量遍历数组,比如i
    • 检查所有奇数长度子数组,通过设置i – 1 ,比如lefti + 1 ,比如right ,作为子数组的开始和结束索引。
    • 通过减去a[left] * b[left] + a[right] * b[right]并添加 a[left] * b[right] + a[right] * b[left] 来更新总数
    • 对于每个子数组,在更新total 后,检查它是否是获得的最小值。相应地更新和存储最小产品。
  • 类似地,检查所有偶数长度子数组并计算总和。
  • 最后,打印得到的数组和最小和。

下面是上述方法的实现:

Java
// Java Program to implement the above approach
  
import java.io.*;
import java.util.*;
  
public class Main {
  
    // Function to calculate the
    // minimum product of same-indexed
    // elements of two given arrays
    static void minimumProdArray(long a[],
                                 long b[], int l)
    {
        long total = 0;
  
        // Calculate initial product
        for (int i = 0; i < a.length; ++i) {
            total += a[i] * b[i];
        }
  
        long min = Integer.MAX_VALUE;
  
        int first = 0;
        int second = 0;
  
        // Traverse all odd length subarrays
        for (int i = 0; i < a.length; ++i) {
  
            int left = i - 1;
            int right = i + 1;
            long total1 = total;
            while (left >= 0 && right < a.length) {
  
                // Remove the previous product
                total1 -= a[left] * b[left]
                          + a[right] * b[right];
  
                // Add the current product
                total1 += a[left] * b[right]
                          + a[right] * b[left];
  
                // Check if current product
                // is minimum or not
                if (min >= total1) {
  
                    min = total1;
                    first = left;
                    second = right;
                }
  
                --left;
                ++right;
            }
        }
  
        // Traverse all even length subarrays
        for (int i = 0; i < a.length; ++i) {
  
            int left = i;
            int right = i + 1;
            long total1 = total;
  
            while (left >= 0 && right < a.length) {
  
                // Remove the previous product
                total1 -= a[left] * b[left]
                          + a[right] * b[right];
  
                // Add to the current product
                total1 += a[left] * b[right]
                          + a[right] * b[left];
  
                // Check if current product
                // is minimum or not
                if (min >= total1) {
                    min = total1;
                    first = left;
                    second = right;
                }
  
                // Update the pointers
                --left;
                ++right;
            }
        }
  
        // Reverse the subarray
        if (min < total) {
  
            reverse(first, second, a);
  
            // Print the subarray
            print(a, b);
        }
  
        else {
            print(a, b);
        }
    }
  
    // Function to reverse the subarray
    static void reverse(int left, int right,
                        long arr[])
    {
        while (left < right) {
            long temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            ++left;
            --right;
        }
    }
  
    // Function to print the arrays
    static void print(long a[], long b[])
    {
        int minProd = 0;
  
        for (int i = 0; i < a.length; ++i) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
        for (int i = 0; i < b.length; ++i) {
            System.out.print(b[i] + " ");
            minProd += a[i] * b[i];
        }
        System.out.println();
        System.out.println(minProd);
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4;
        long a[] = { 2, 3, 1, 5 };
        long b[] = { 8, 2, 4, 3 };
  
        minimumProdArray(a, b, n);
    }
}


输出:
1 3 2 5 
8 2 4 3 
37

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

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