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

今天偶然看到一个JAVA算法题，整个网上居然找不到JAVA版的答案，于是自己花了几个小时把它搞定了。



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]

[5, 9]
[8, 3]

[5, 9, 1]
[8, 3, 4]

[5, 9, 1, 12]
[8, 3, 4, 7]

[5, 9, 1, 12, 19]
[8, 3, 4, 7, 2]

[5, 9, 1, 12, 11, 2]
[8, 3, 4, 7, 19, 3]

08-26 6887

07-24 1万+
05-14 8732
09-15 6217
08-30 1039
08-06 2万+
08-28 4999
08-26 2056
12-26
03-23 2013
01-16 2202
03-06 7万+
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林