【发布时间】:2020-02-24 01:28:30
【问题描述】:
leetcode.com 向我提出了问题
问题陈述:
给定一个由 2n 个整数组成的数组,您的任务是将这些整数分组为 n 对整数,例如 (a1, b1), (a2, b2), ..., (an, bn),它们的总和为 min (ai, bi) 对从 1 到 n 的所有 i 尽可能大。
示例 1: 输入:[1,4,3,2]
输出:4
解释:n为2,对的最大和为4 = min(1, 2) + min(3, 4)。
注意: n 为正整数,取值范围为 [1, 10000]。 数组中的所有整数都将在 [-10000, 10000] 的范围内。
我已尝试使用以下 javascript 代码解决此问题
// NOTE: This is more optimal and can work
function chunkWithoutIndex(inputArr, partition) {
let length = inputArr.length;
let sliced = [];
let count = 0;
for (let i = 0; i <= length - 1; i++) {
let subArr = sliced[count];
if (!subArr) {
sliced[count] = [];
}
let subArrLen = sliced[count].length;
if (subArrLen !== partition) {
sliced[count].push(inputArr[i]);
} else {
count++;
sliced[count] = [inputArr[i]]
}
}
return sliced;
}
// NOTE: This does not consider the chunk size
function checkWithTwoPointers(inputArr) {
let length = inputArr.length;
let left = 0;
let right = length - 1;
let sliced = [];
while (left <= right) {
if (left !== right) {
sliced.push([inputArr[left], inputArr[right]]);
} else {
sliced.push([inputArr[left] || inputArr[right]]);
}
left++;
right--;
}
return sliced;
}
function arrayPartition(inputArr, partition) {
// let sliced = chunkWithoutIndex(inputArr, partition);
let sliced = checkWithTwoPointers(inputArr);
let sum = 0;
let slicedLen = sliced.length;
for (let i = 0; i <= slicedLen - 1; i++) {
sum = sum + Math.min(...sliced[i]);
}
return sum;
}
在提交问题以供接受时,由于不同的测试用例而失败。
看到输入 = [1, 4, 3, 2] 的测试用例运行良好,它期望输出为 4。 所以配对是
(1, 4) , (3, 2) = 1 + 2 = 3
(1, 3), (4, 2) = 1 + 2 = 3
(1, 2), (4, 3) = 1 + 3 = 4 --> Selected Pair
还有一个测试用例 input = [1, 1, 2, 2] ,它期望输出是 3?
如果我使用上面编写的相同函数,它将创建一对 (1,2) , (1, 2) => 1+ 1 = 2。但他们现在期待这对 (1,1), ( 2, 2) => 1 + 2 = 3。
如何解决这个问题?我在这里错过了什么吗?
【问题讨论】:
标签: javascript python arrays algorithm