我目前正在处理一个函数,该函数需要一个数字数组作为输入并输出一个就是最大附加值的整数
catch:如果选择了num [i],并且num [i-1]和/或num [i + 1]与num [i]具有相同的值,则所有具有num [i]邻居的邻近数字]将被删除。
如果选择要删除任何元素并将其添加到总和中,则等于邻元素之一的任何其他元素都将被删除而不会添加到得分中。在第一种情况下,通过删除2(索引1),它的邻居为2,因此所有2将在一个回合中被删除。
例如,如果我们有0,2,2,2,7,2,2,2。输出应为2 + 7 = 9,其他2个都将被删除
但是,我陷入了案例3和5,我解决了案例1,2和4
任何输入将不胜感激
function findMaxScore(nums){
var curr_max = 0;
var countNeg = 0;
//edge case: if all numbers are < 0, return 0
let allNeg = nums => nums.every(n => n <= 0);
if (nums.length === 0 || allNeg(nums)){
console.log("All numbers in nums are negative or =0, thus return 0");
return 0;}
else{
for (var i = 0; i<nums.length; i++){
//if there is a 0 in the array or a negative number, move on
//since they don't add any positive values
if(nums[i]<0 || nums[i]===0){
nums.splice(i,1);
console.log("got rid of all the 0s and negative numbers, currently num is: "+ nums);
}
var leftNeighbor = nums[i-1];
//console.log("left neighbor: "+leftNeighbor);
var rightNeighbor = nums[i+1];
//console.log("right neighbor: "+rightNeighbor);
if(leftNeighbor<=0){
nums.splice(i-1,1);
}
if(rightNeighbor<=0){
nums.splice(i+1,1);
}
curr_max+= nums[i];
//delete all neighboring numbers that contain the same value as i-1 and i+1
//if the left or ride side of subarray contains the same neighboring value,
//delete that number as well
if(nums[i]===leftNeighbor){
nums.splice(i-1,1);
if(nums[i-2]===leftNeighbor){
nums = nums.filter(n => n !== leftNeighbor);
}
}
else if(nums[i]===rightNeighbor){
nums.splice(i+1,1);
if(nums[i+2]===rightNeighbor){
nums = nums.filter(n => n !== rightNeighbor);
}
}
if(nums.length=1){
curr_max+= nums[i];
} return curr_max;
console.log("current max: "+curr_max);
//if no nums[i-1] or nums[i+1] is not equal to nums[i]
//delete the neighboring numbers
if((leftNeighbor!=nums[i]&&rightNeighbor!=nums[i])&&nums.length>1){
nums.splice(i-1,1);
nums.splice(i,1);
i++;
//console.log("iteration number: "+(i+1)+". num is currently at index "+(i)+", with a value of: "+nums[i]);
//console.log("deleting value at nums[i-1]: "+ nums[i-1]+ ",index of: "+ (i-1));
//console.log("deleting value at nums[i+1]: "+ nums[i]+ ",index of: "+ (i));
curr_max+= nums[i];
}
}
}
/*
//check to see if the current score is the max score
var max_so_far = curr_max;
for (var j = i; j< nums.length; j++){
curr_max += (nums[j]-nums[j-1]);
max_so_far = Math.max(curr_max,max_so_far);
}
return max_so_far;
*/
return curr_max;
}
console.log("result one: "+findMaxScore([0,1,1,5,1,1])+", expected value 6");//1+5
console.log("result two: "+findMaxScore([1,1,1,1,1,1,1,14,1,1])+", expected value 14");//1+14
console.log("result three: "+findMaxScore([-3,-7,3,2,3,4,3,0])+", expected value 9");//expected 9 3+3+3
console.log("result four: "+findMaxScore([-1,-7,-9])+", expected value 0");//expected 0
console.log("result five: "+findMaxScore([0,0,0,1,2,3,4,5,6,7,8,9,10])+", expected value 30");//expected 30 10+8+6+4+2
我的输出:
got rid of all the 0s and negative numbers, currently num is: 1,1,4,1,1
result one: 5, expected value 5
result two: 11, expected value 11
result three: 2, expected value 9
All numbers in nums are negative or =0, thus return 0
result four: 0, expected value 0
got rid of all the 0s and negative numbers, currently num is: 1,2,3,4,5,6,7,8,9,10
result five: 2, expected value 30
答案 0 :(得分:3)
除第二个灵魂灵魂获得15
而不是14
的最后一个注释值之外,该灵魂元素获得所有期望值。
工作原理:
它采用迭代和递归的方法,因为它通过交出数组和访问/删除值的部分和来迭代每个项目和递归。
与任何工作的递归函数一样,此递归设计在顶部采用退出条件,在此条件下,检查数组的剩余长度并取最大值作为结果,并返回函数
iter
。然后发生迭代部分,其中对总和取值
v
,并从中过滤出数组
- 左邻居的值
- 来自正确邻居的值和
- 从获取的索引处的值开始。
然后再次调用
iter
,其中包含一个子集和一个和。
function findMaxScore(array) {
function iter(array, sum) {
if (!array.length) {
max = Math.max(max, sum);
return;
}
array.forEach((v, i, a) =>
iter(
array.filter((w, j) => w !== a[i - 1] && w !== a[i + 1] && i !== j),
sum + v
)
);
}
var max = 0;
iter(array.filter(v => Number.isInteger(v) && v > 0), 0);
return max;
}
console.log("result one: " + findMaxScore([0, 1, 1, 5, 1, 1]) + ", expected value 6");//1+5
console.log("result two: " + findMaxScore([1, 1, 1, 1, 1, 1, 1, 14, 1, 1]) + ", expected value 14");//1+14
console.log("result three: " + findMaxScore([-3, -7, 3, 2, 3, 4, 3, 0]) + ", expected value 9");//expected 9 3+3+3
console.log("result four: " + findMaxScore([-1, -7, -9]) + ", expected value 0");//expected 0
console.log("result five: " + findMaxScore([0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) + ", expected value 30");//expected 30 10+8+6+4+2