解决一个阿里在线Java笔试编程题

今天偶然看到一个JAVA算法题,整个网上居然找不到JAVA版的答案,于是自己花了几个小时把它搞定了。
使用了动态规划,并把每个子问题的最优解做了缓存,大大降低了时间复杂度。欢迎各位小伙伴留言!

题目
已知有两个长度相等的正整数数组A、B,
其元素分别记为{a(1),a(2),a(3),a(4) … a(n) …}和{b(1),b(2),b(3),b(4) … b(n) …},
两个数组中任一元素大小不超过10000,数组长度在1000以内,
现在按照以下规则将A数组中的元素插入到B数组中进行合并:
对于A数组中任一元素可以插入到B数组中任意位置。
对于A数组中任一元素a(i),在合并之后的数组中a(i)对应的下标小于a(i+1)对应的下标。(如果a(i+1)存在的话)
问题:
对合并之后的数组中相邻元素两两相乘,然后求其累加值,请给出所有合并可能形成的数组对应累加值的最小值。
以长度为4的两个数组为例:
A:{a1,a2,a3,a4}
B:{b1,b2,b3,b4}
插入完成后的数组可能为:
{a1,a2,b1,b2,b3,b4,a3,a4}、
{b1,a1,b2,b3,a2,a3,b4,a4}等。
其对应的累加值分别对应为
a1a2 + b1b2 + b3b4 + a3a4、
b1a1 + b2b3 + a2a3 + b4a4等。

实现代码:


import java.util.Arrays;

public class AliMulti {

    public static int getMinMulti(int arr1[], int s1, int e1, int arr2[], int s2, int e2, int cache[][]) {
        if (s1 > e1 && s2 > e2) {
            return 0;
        }
        if (s1 < arr1.length && s2 < arr2.length) {
            if (cache[s1][s2] > 0) {
                return cache[s1][s2];
            }
        }
        if (s1 > e1) {
            int temp = 0;
            for (int j = s2; j < e2; j++, j++) {
                temp = temp + arr2[j] * arr2[j + 1];
            }
            return temp;
        }
        if (s2 > e2) {
            int temp = 0;
            for (int j = s1; j < e1; j++, j++) {
                temp = temp + arr1[j] * arr1[j + 1];
            }
            return temp;
        }
        if (s1 == e1 && s2 == e2) {
            cache[s1][s2] = arr1[s1] * arr2[s2];
            return arr1[s1] * arr2[s2];
        }
        int res = 0;
        int result1 = 0;
        if (s1 + 1 <= e1) {
            result1 = arr1[s1] * arr1[s1 + 1] + getMinMulti(arr1, s1 + 2, e1, arr2, s2, e2, cache);
        }
        int group[] = new int[(e2 - s2) / 2 + 1];
        for (int i = s2; i <= e2; i = i + 2) {
            int temp = 0;
            for (int j = s2; j < i; j++, j++) {
                temp = temp + arr2[j] * arr2[j + 1];
            }
            int x = (i - s2) / 2;
            group[x] = temp + arr1[s1] * arr2[i] + getMinMulti(arr1, s1 + 1, e1, arr2, i + 1, e2, cache);
        }
        Arrays.sort(group);
        if (result1 > 0) {
            res = result1 < group[0] ? result1 : group[0];
        } else {
            res = group[0];
        }
        cache[s1][s2] = res;
        return res;
    }

    public static void main(String[] args) {
        int arr1[] = {5};
        int arr2[] = {8};
        System.out.println("输入:");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        System.out.println("最小值: " + getMinMulti(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1,new int[arr1.length][arr1.length]));

        arr1 = new int[]{5, 9};
        arr2 = new int[]{8, 3};
        System.out.println("输入:");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        System.out.println("最小值: " + getMinMulti(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1,new int[arr1.length][arr1.length]));

        arr1 = new int[]{5, 9, 1};
        arr2 = new int[]{8, 3, 4};
        System.out.println("输入:");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        System.out.println("最小值: " + getMinMulti(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1,new int[arr1.length][arr1.length]));

        arr1 = new int[]{5, 9, 1, 12};
        arr2 = new int[]{8, 3, 4, 7};
        System.out.println("输入:");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        System.out.println("最小值: " + getMinMulti(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1,new int[arr1.length][arr1.length]));

        arr1 = new int[]{5, 9, 1, 12, 19};
        arr2 = new int[]{8, 3, 4, 7, 2};
        System.out.println("输入:");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        System.out.println("最小值: " + getMinMulti(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1,new int[arr1.length][arr1.length]));

        arr1 = new int[]{5, 9, 1, 12, 11, 2, 45, 15, 34, 43, 14, 556, 56, 98, 34, 66, 99, 12, 56, 243, 98, 34, 66, 99, 12};
        arr2 = new int[]{8, 3, 4, 17, 19, 3, 59, 34, 13, 34, 3, 456, 243, 98, 34, 66, 99, 12, 56, 243, 98, 34, 66, 99, 12};
        System.out.println("输入:");
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        int ray[][]=new int[arr1.length][arr1.length];
        System.out.println("最小值: " + getMinMulti(arr1, 0, arr1.length - 1, arr2, 0, arr2.length - 1, ray));
        for (int[] each : ray) {
            System.out.println(Arrays.toString(each));
        }
    }
}

测试结果如下:
输入:
[5]
[8]
最小值: 40
输入:
[5, 9]
[8, 3]
最小值: 67
输入:
[5, 9, 1]
[8, 3, 4]
最小值: 53
输入:
[5, 9, 1, 12]
[8, 3, 4, 7]
最小值: 107
输入:
[5, 9, 1, 12, 19]
[8, 3, 4, 7, 2]
最小值: 145
输入:
[5, 9, 1, 12, 11, 2]
[8, 3, 4, 7, 19, 3]
最小值: 172

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页