最小的正整数,不在数组中出现。

huangapple go评论67阅读模式
英文:

Smallest positive integer that does not occur in an array

问题

I am attempting this question using javascript. When I run my implementation on the website I get 11%, which asides from poor runtime states that I fail four hidden tests.

我正在尝试使用 JavaScript 回答这个问题。当我在网站上运行我的实现时,得到了11%,除了性能较差之外,还显示我未通过四个隐藏测试。

I cannot understand how my implementation is failing. I understand its not a great implementation and there are cleaner working answers in the linked question, but I am still curious what false logic I am using.

我无法理解我的实现为何失败。我明白它不是一个很好的实现,而且在链接的问题中有更干净的工作答案,但我仍然好奇 我使用了什么错误的逻辑

  A = A.filter(a => a > 0).sort()

  if (A.length === 0) {
    return 1
  }
  
  if (A.length === 1) {
    return A[0] + 1
  }

  for (let i = 0; i < A.length; i++) {
    let curr = A[i]
    let next = A[i + 1]

    if (curr !== next && (curr + 1) !== next) {
      return curr + 1
    }
  }

My logic is that since I am traversing a sorted array of positive integers from lowest to highest, if the current element + 1 doesn't equal the following element (i.e. [5,7] would be 5 + 1 != 7), there is a gap, which is the missing integer.

我的逻辑是,由于我正在遍历从最低到最高的正整数排序数组,如果当前元素 + 1 不等于下一个元素(例如 [5,7] 就是 5 + 1 != 7),则存在一个间隙,即缺失的整数。

And if the loop gets all the way to the end, the last if statement still triggers since next is undefined, which would return the max value + 1.

如果循环一直执行到最后,由于下一个元素是 undefined,最后的 if 语句仍然会触发,这将返回最大值 + 1。

英文:

I am attempting this question using javascript. When I run my implementation on the website I get 11%, which asides from poor runtime states that I fail four hidden tests.

I cannot understand how my implementation is failing. I understand its not a great implementation and there are cleaner working answers in the linked question, but I am still curious what false logic I am using.

  A = A.filter(a => a > 0).sort()

  if (A.length === 0) {
    return 1
  }
  
  if (A.length === 1) {
    return A[0] + 1
  }

  for (let i = 0; i < A.length; i++) {
    let curr = A[i]
    let next = A[i + 1]

    if (curr !== next && (curr + 1) !== next) {
      return curr + 1
    }
  }

My logic is that since I am traversing a sorted array of positive integers from lowest to highest, if the current element + 1 doesn't equal the following element (i.e. [5,7] would be 5 + 1 != 7), there is a gap, which is the missing integer.

And if the loop gets all the way to the end, the last if statement still triggers since next is undefined, which would return the max value + 1.

答案1

得分: 1

Instead of sorting the array, just look for the max of the array and use that within your function. Note that instead of working on arrays, you could use sets. The set .has method is faster than include method.

function  smallest_positive(ar){
    let set = new Set(ar)
    let mx = Math.max(...set);
    for(let i = 1; i < mx; i++) 
        if(!set.has(i)) return i;
    return mx < 1 ? 1 : mx + 1;
}; 

console.log(smallest_positive([-1,4,2,-3]))
console.log(smallest_positive([1, 2, 3]))
console.log(smallest_positive([5,7]))
console.log(smallest_positive([1, 3, 6, 4, 1, 2]))
英文:

Instead of sorting the array, just look for the max of the array and use that within your function. Note that instead of working on arrays, you could use sets. The set .has method is faster than include method.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function  smallest_positive(ar){
    let set = new Set(ar)
    let mx = Math.max(...set);
    for(let i = 1; i &lt; mx; i++) 
        if(!set.has(i)) return i;
    return mx &lt; 1 ? 1: mx + 1;
}; 

console.log(smallest_positive([-1,4,2,-3]))
console.log(smallest_positive([1, 2, 3]))
console.log(smallest_positive([5,7]))
console.log(smallest_positive([1, 3, 6, 4, 1, 2]))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月17日 03:48:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定