创建两个数组,存放两个人捡苹果的数据,最后从这个两个数据中取出两个不相邻的相加,与以前的比较,最后得出最大值,
public class Solution {/*** @param a: a list of integer* @param k: a integer* @param l: a integer* @return: return the maximum number of apples that they can collect.*/public int pickApples(int[] a, int k, int l) {// write your code hereif (k + l > a.length) {return -1;}int[] answerk = new int[a.length];int[] answerl = new int[a.length];for (int i = 0; i < k; i++) {answerk[k - 1] += a[i];}for (int i = 0; i < l; i++) {answerl[l - 1] += a[i];}for (int i = k; i < a.length; i++) {answerk[i] = answerk[i - 1] + a[i] - a[i - k];}for (int i = l; i < a.length; i++) {answerl[i] = answerl[i - 1] + a[i] - a[i - l];}int answer = 0;for (int i = k - 1; i < answerk.length; i++) {for (int j = l - 1; j < answerl.length; j++) {if (i - j >= k || j - i >= l)answer = Math.max(answerk[i] + answerl[j], answer);}}return answer;}
}