在没有调试的情况下,此解决方案在 Codility 上的任务得分为 100%(正确性和性能均为 100%):
function solution(A) {
var sum_right = 0;
for (int of A.slice(1)) {
sum_right += int;
}
var sum_left = A[0];
var diff_of_sums = sum_left - sum_right;
var lowest_diff = Math.abs(diff_of_sums);
var diff_new;
// we assume the length is at least 2
if (A.length == 2) {
return lowest_diff;
}
for (var int of A.slice(1)) {
diff_new = Math.abs(sum_left - sum_right);
if (diff_new < lowest_diff) {
lowest_diff = diff_new;
}
sum_left += int;
sum_right -= int;
}
return lowest_diff;
}
带调试:
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var sum_right = 0;
for (int of A.slice(1)) {
sum_right += int;
}
var sum_left = A[0];
var diff_of_sums = sum_left - sum_right;
// var total = Math.abs(sum_left + sum_right);
var lowest_diff = Math.abs(diff_of_sums);
var diff_new;
// we assume the length is at least 2
if (A.length == 2) {
return lowest_diff;
}
// console.log("lowest_diff initially:", lowest_diff)
// var diff_of_sums_new = diff_of_sums;
// console.log("diff_of_sums initially:", diff_of_sums)
// console.log("A.slice(1):", A.slice(1))
for (var int of A.slice(1)) {
// console.log("lowest_diff", lowest_diff)
diff_new = Math.abs(sum_left - sum_right);
if (diff_new < lowest_diff) {
lowest_diff = diff_new;
}
sum_left += int;
sum_right -= int;
}
// if (Math.abs(sumleft - sumright)<ans)
// {
// ans = Math.abs(sumleft - sumright);
// }
// sumleft += A[P];
// sumright -=A[P];
// // console.log("int === -1:", int === -1);
// // diff_of_sums = diff_of_sums_new;
// console.log("lowest_diff =", lowest_diff);
// // console.log("A[index + 1] =", A[parseInt(index) + 1]);
// // console.log("parseInt(index) === 1", parseInt(index) === 1)
// diff_of_sums = Math.abs(lowest_diff - 2 * Math.abs(int));
// // console.log("diff_of_sums =", diff_of_sums);
// // console.log("diff_of_sums = Math.abs(diff_of_sums - 2 * A[index + 1]) = ", diff_of_sums_new);
// if (diff_of_sums < lowest_diff) {
// lowest_diff = diff_of_sums;
// // console.log("lowest_diff = diff_of_sums =", diff_of_sums_new)
// } else {
// return lowest_diff;
// }
// }
// console.log("lowest_diff before returning", lowest_diff);
return lowest_diff;
}
// Note that it's better to use test cases in Codility for this, but I've left here to show some.
// console.log("solution([-1000, 1000])", solution([-1000, 1000]));
// console.log("solution([2, 7, 20, 30, 1])", solution([2, 7, 20, 30, 1])); // sum 60, smallest diff = |29 - 31| = 2
// console.log("solution([-2, -7, -20, -30, -1])", solution([-2, -7, -20, -30, -1])); // sum -60, smallest diff = 2
// console.log("solution([-1, -1]):", solution([-1, -1]));
// console.log("solution([-2, -1]):", solution([-2, -1]));
// console.log("solution([-2, -1, -3]):", solution([-2, -1, -3]));
// console.log("solution([]):", solution([]))
最初我尝试从中途开始,但这使实现更加复杂。这是我在放弃这种方法之前想出的(而且我不会为破解解决方案而烦恼):
function solution(A) {
// const sum = A.reduce((partial_sum, a) => partial_sum + a);
// console.log(sum);
var size = A.length;
if (size % 2 == 0) {
mid = size/2;
} else {
mid = Math.floor(size/2);
}
console.log("mid initially", mid);
var sum1 = A.slice(0, mid).reduce((partial_sum, a) => partial_sum + a);
// console.log("sum1:",sum1);
var sum2 = A.slice(mid).reduce((partial_sum, a) => partial_sum + a);
// console.log("sum2:", sum2);
var sum_diff = sum1 - sum2;
// console.log("sum_diff:", sum_diff);
if (sum_diff === 0) {
return sum_diff;
}
// sum_diff = function() {Math.abs(sum2 - sum1)};
// sum_diff = sum_diff();
var lowest_diff = Math.abs(sum_diff);
var diff_negative = (sum_diff < 0);
console.log("diff_negative initially:", diff_negative)
var crossed_over = false;
var sum_diff_new;
while (diff_negative) {
mid++;
if (mid === size) {
return lowest_diff;
}
// var sum1_new = sum1 + A[mid];
// var sum2_new = sum2 - A[mid];
// sum_diff_new = sum1_new - sum2_new = sum1 - sum2 + 2*A[mid] = sum_diff - 2*A[mid];
sum_diff_new = sum_diff - 2*A[mid];
diff_negative = (sum_diff_new < 0);
if (diff_negative = false) {
crossed_over = true;
if (lowest_diff <= sum_diff_new) {
return lowest_diff;
} else {
return sum_diff_new;
}
}
}
while(!diff_negative) {
mid--;
if (mid === -1) {
return lowest_diff;
}
// var sum1_new = sum1 - A[mid];
// var sum2_new = sum2 + A[mid];
// sum_diff_new = sum1_new - sum2_new = sum1 - sum2 - 2*A[mid] = sum_diff - 2*A[mid];
console.log("sum_diff:", sum_diff);
sum_diff_new = sum_diff + 2*A[mid];
console.log("sum_diff_new:", sum_diff_new);
diff_negative = (sum_diff_new < 0);
if (diff_negative = true) {
crossed_over = true;
var sum_diff_new_pos = Math.abs(sum_diff_new);
if (lowest_diff <= sum_diff_new_pos) {
return lowest_diff;
} else {
return sum_diff_new_pos;
}
}
}
}
// Issues: doesn't work e.g. with [-2, -1, -3] and [-2, -7, -20, -30, -1]