foo.bar Power-Hungry任务:Java代码仅未通过测试4求助
Lambda指挥官的空间站规模极大,这类大型空间站需要消耗大量能源,搭载末日装置的空间站耗能更是惊人。为满足能源需求,Lambda指挥官在空间站外表面安装了太阳能电池板,但空间站处于类星体量子通量场中,这会严重损坏太阳能电池板。你和手下被指派修复电池板,但为维持空间站供电,希望尽可能不一次性拆除所有电池板!
你需要确定在给定电池板阵列中,可离线维修的电池板集合,同时保持阵列的最大输出功率。为此,首先要计算每个阵列的最大输出功率。请编写函数solution(xs),该函数接收一个表示阵列中各电池板功率输出值的整数列表,返回该列表某个非空子集的最大乘积的字符串形式。例如,若阵列包含功率值为(2, -3, 1, 0, -5)的电池板,最大乘积来自子集xs[0]=2、xs[1]=-3、xs[4]=-5,乘积为2*(-3)*(-5)=30,因此solution([2,-3,1,0,-5])返回"30"。
每个电池板阵列包含至少1个、至多50个电池板,每个电池板的功率输出值绝对值不超过1000(部分电池板故障严重,会消耗能量,但你可利用电池板的波稳定器技巧,将两个负输出电池板组合,产生其功率值乘积的正输出)。最终乘积可能极大,需以数字的字符串形式返回结果。
你的代码应通过以下测试用例,同时可能会针对未展示的隐藏测试用例运行。
-- Python用例 --
输入:solution.solution(2, 0, 2, 2, 0)
输出:8
输入:solution.solution(-2, -3, 4, -5)
输出:60
-- Java用例 --
输入:Solution.solution({2, 0, 2, 2, 0})
输出:8
输入:Solution.solution({-2, -3, 4, -5})
输出:60
以下是我的Java代码,自定义测试用例均可轻松通过:
public static void main(String[] args) { int[] array1 = {2, 0, 2, 2, 0}; // 8 int[] array2 = {-2, -3, 4, -5}; // 60 int[] array3 = {0}; // 0 int[] array4 = {1};// 1 int[] array5 = {0, 2, 2, -4, -2, -2};// 32 int[] array6 = {1, 1, 2, 1, 1};// 2 int[] array7 = {-3, 4, -5, -2};// 60 int[] array8 = {0, -1};// 0 int[] array9 = {0, 1};// 1 int[] array10 = {4, 3, -5, -4, -5, -2, 0, 0, 1, 1, -2};// 2400 System.out.println(solution(array1)); System.out.println(solution(array2)); System.out.println(solution(array3)); System.out.println(solution(array4)); System.out.println(solution(array5)); System.out.println(solution(array6)); System.out.println(solution(array7)); System.out.println(solution(array8)); System.out.println(solution(array9)); System.out.println(solution(array10)); }
public static String solution(int[] xs) { int length = xs.length; int negativeCount = 0; int positiveCount = 0; int[] negativeArray = new int[length]; int[] positiveArray = new int[length]; long multiply = 1; if (length == 0) { return "0"; } if (length == 1) { return String.valueOf(xs[0]); } for (int number : xs) { if (number < 0) { negativeArray[negativeCount] = number; negativeCount++; } else if (number > 0) { positiveArray[positiveCount] = number; positiveCount++; } } if ((positiveCount == 0 && negativeCount == 0) || (positiveCount == 0 && negativeCount == 1)) { return "0"; } if (negativeCount % 2 == 1) { int largestNegativeNumber = Integer.MIN_VALUE; int largestNegativeIndex = -1; for (int i = 0; i < negativeArray.length; i++) { if (negativeArray[i] < 0 && negativeArray[i] > largestNegativeNumber) { largestNegativeNumber = negativeArray[i]; largestNegativeIndex = i; } } if (largestNegativeIndex != -1) { int[] newArray = new int[negativeArray.length - 1]; int j = 0; for (int i = 0; i < negativeArray.length; i++) { if (i != largestNegativeIndex) { newArray[j++] = negativeArray[i]; } } negativeArray = newArray; negativeCount--; } } for (int i = 0; i < positiveCount + negativeCount; i++) { if (i < positiveCount) { multiply *= positiveArray[i]; } else { multiply *= negativeArray[i - positiveCount]; } } return Long.toString(multiply); }
我已多次修改代码,目前foo.bar提供的5个测试用例中,仅第4个无法通过。我尝试查找相关信息,但仅找到Python实现的代码,对比后未发现我的Java代码存在逻辑遗漏,请求帮助排查问题!
内容的提问来源于stack exchange,提问作者lubo dimoff

