首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果删除数组,则检查数组是否包含增量元素

如果删除数组,则检查数组是否包含增量元素
EN

Stack Overflow用户
提问于 2022-02-03 18:22:20
回答 3查看 150关注 0票数 1

如果我们要从数组中删除一个元素,我想编写一个函数来检查数组是否是增量的。因此,例如,一个类似于

代码语言:javascript
运行
复制
[1,3,4,2,8]

返回true,因为如果删除2,数组将是增量的,即1,3,4,8

为了解决这个问题,我创建了一个遍历数组的函数,检查n是否小于n+1,创建一个包含真值和假值的数组,这取决于我们正在迭代的元素是否满足所述条件。

在n+1小于或等于n的情况下,我有另一个检查n+1是否小于n-1的条件,在这个实例中返回“非常假”。

然后,如果我的新Array指示删除一个元素不会给我留下一个增量值数组,我想返回false。我通过检查是否;)我的新数组包含多个假值ii。)我的新数组只包含‘非常假’的值,如果非常假不是数组iii中的最后一个元素的话。)我的数组包含非常假的和假的。

努力让这个逻辑起作用。我增加了几个测试用例--我很难找到正确的答案。

代码语言:javascript
运行
复制
function solution(sequence) {
    let newArr = [];
    for(let i = 0; i < sequence.length - 1; i++) {
        sequence[i] < sequence[i+1] ? newArr.push(true) 
            : sequence[i+1] <= sequence[i-1] ? newArr.push('very false')
                :newArr.push(false)
    }
    return (newArr.filter(i => i === false).length) > 1 || ( (newArr.includes('very false')) && (newArr[newArr.length - 1] !== 'very false')) || ( newArr.includes(false) && newArr.includes('very false')) ? false : true;
}
//TEST CASES
let one = [1, 2, 3, 4, 5, 3, 5, 6] //I get this right
let two = [40, 50, 60, 10, 20, 30] //I get this right
let three = [1, 2, 3, 4, 3, 6] //I get this wrong
let four = [1, 4, 10, 4, 2]  // I get this wrong


//EXPECTED OUTCOMES
console.log(solution(one)) => false;
console.log(solution(two)) => false;
console.log(solution(three)) => true;
console.log(solution(four)) => false;

FYI:这不是为了作业或考试。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-02-03 19:31:22

创建一个新数组似乎过分了。首先,我将定义一个函数来验证给定数组的索引是否是增量的。

然后检查在那之后是否有第二次违规。如果是,则返回false。

如果只发生一次冲突,则在数组中的前两个或最后两个值之间发生时返回true。

在所有其他情况下,请验证是否删除两个涉及的值中的任何一个解决了冲突。

代码:

代码语言:javascript
运行
复制
// Return the index where the value is not greater than its predecessor
function violation(a, start=0) {
    for (let i = start + 1; i < a.length; i++) {
        if (a[i - 1] >= a[i]) return i;
    }
    return 0;
}

function solution(a) {
    let i = violation(a);
    return !i || !violation(a, i) && (i==1 || i==a.length-1 
                                     || a[i-2] < a[i] || a[i-1] < a[i+1]);
}

//TEST CASES
let tests = [
    [1, 2, 3, 4, 5, 3, 5, 6],
    [40, 50, 60, 10, 20, 30],
    [1, 2, 3, 4, 3, 6],
    [1, 4, 10, 4, 2]
];

for (let test of tests) {
    console.log(solution(test));
}

票数 1
EN

Stack Overflow用户

发布于 2022-02-03 20:24:08

下一个提供的解决方案使用了一种基于every的方法,在这种方法中,人们可以计算可能的增量数组违反OP的标准/要求的数量。根据是否发生了冲突,我们会将当前值与导致违规的最新值进行比较,或者仅仅比较两个连续的数组项。

一旦超过允许的最大冲突计数,every立即退出/中断其迭代,使用false布尔返回值,该值也将成为下面实现的canBeMadeIncremental函数的返回值;否则,这两个进程的返回值都是true

代码语言:javascript
运行
复制
function canBeMadeIncremental(arr) {
  let violationCount = 0;
  let comparisonValue = null;

  return arr.every((value, idx, arr) => {
    let isAchievable = true;

    if (idx >= 1) {
      comparisonValue = comparisonValue ?? arr[idx - 1];

      if (comparisonValue >= value) {
        ++violationCount;
      } else {
        comparisonValue = null;
      }
      isAchievable = (violationCount <= 1);
    }
    return isAchievable;
  });
}

console.log(
  "canBeMadeIncremental([1, 2, 3, 4, 5, 3, 5, 6]) ?..",
  canBeMadeIncremental([1, 2, 3, 4, 5, 3, 5, 6]) // false
);
console.log(
  "canBeMadeIncremental([40, 50, 60, 10, 20, 30]) ?..",
  canBeMadeIncremental([40, 50, 60, 10, 20, 30]) // false
);
console.log(
  "canBeMadeIncremental([1, 2, 3, 4, 3, 6]) ?..",
  canBeMadeIncremental([1, 2, 3, 4, 3, 6]) // true
);
console.log(
  "canBeMadeIncremental([1, 4, 10, 4, 2]) ?..",
  canBeMadeIncremental([1, 4, 10, 4, 2]) // false
);
代码语言:javascript
运行
复制
.as-console-wrapper { min-height: 100%!important; top: 0; }

上面的代码示例可以很容易地重构成一个更通用的函数,该函数提供关于增量数组是否可能以及如果是,如何实现它的数据(例如,需要删除哪个索引/索引).

代码语言:javascript
运行
复制
function howToMakeIncremental(arr) {
  let isAchievable = true;

  let comparisonValue = null;
  let violationCount = 0;

  const violatingIndexList = [];

  arr.every((value, idx, arr) => {
    if (idx >= 1) {
      comparisonValue = comparisonValue ?? arr[idx - 1];

      if (comparisonValue >= value) {

        violatingIndexList.push(idx);

        ++violationCount;
      } else {
        comparisonValue = null;
      }
      isAchievable = (violationCount <= 1);
    }
    return isAchievable;
  });
  const howTo = { isAchievable };

  if (isAchievable) {
    howTo.violatingIndexList = violatingIndexList;
  }
  return howTo;
}

console.log(
  "howToMakeIncremental([1, 2, 3, 4, 5, 3, 5, 6]) ?..",
  howToMakeIncremental([1, 2, 3, 4, 5, 3, 5, 6]) // { isAchievable: false }
);
console.log(
  "howToMakeIncremental([40, 50, 60, 10, 20, 30]) ?..",
  howToMakeIncremental([40, 50, 60, 10, 20, 30]) // { isAchievable: false }
);
console.log(
  "howToMakeIncremental([1, 2, 3, 4, 3, 6]) ?..",
  howToMakeIncremental([1, 2, 3, 4, 3, 6]) // { isAchievable: true, violatingIndexList: [4] }
);
console.log(
  "howToMakeIncremental([1, 4, 10, 4, 2]) ?..",
  howToMakeIncremental([1, 4, 10, 4, 2]) // { isAchievable: false }
);
代码语言:javascript
运行
复制
.as-console-wrapper { min-height: 100%!important; top: 0; }

票数 2
EN

Stack Overflow用户

发布于 2022-02-03 18:57:51

您的逻辑没有什么,只是删除第一个返回。

代码语言:javascript
运行
复制
function solution(sequence) {
let newArr = [];
for(let i = 0; i < sequence.length - 1; i++) {
    sequence[i] < sequence[i+1] ? newArr.push(true) 
        : sequence[i+1] < sequence[i-1] ? newArr.push('very false')
            :newArr.push(false)
}
// return newArr
return (newArr.filter(i => i === false).length) > 1 || ( (newArr.includes('very 
    false')) && (newArr[newArr.length - 1] !== 'very false')) || ( 
    newArr.includes(false) && newArr.includes('very false')) ? false : true;
}
//TEST CASES
let one = [1, 2, 3, 4, 5, 3, 5, 6]
let two = [40, 50, 60, 10, 20, 30]
let three = [1, 2, 3, 4, 3, 6] 
let four = [1, 4, 10, 4, 2]  

console.log(solution(one));
console.log(solution(two));
console.log(solution(three));
console.log(solution(four));

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70976394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档