使数组连续

时间:2017-02-21 07:31:38

标签: javascript

我陷入了codeFights.my代码中的挑战,通过了简单的测试,在5个隐藏测试中只有2个失败,她就是挑战指令:

Ratiorg为他的生日从CodeMaster获得了不同大小的雕像作为礼物,每个雕像都有一个非负整数。由于他喜欢把事情做得很完美,所以他希望将它们从最小到最大排列,这样每个雕像都会比前一个雕像精确地大1个。他可能需要一些额外的雕像来完成它。帮助他找出所需的最少额外雕像数量。

Example

For statues = [6, 2, 3, 8], the output should be
makeArrayConsecutive2(statues) = 3.

Ratiorg needs statues of sizes 4, 5 and 7.

Input/Output

[time limit] 4000ms (js)
[input] array.integer statues

An array of distinct non-negative integers.

Constraints:
1 ≤ statues.length ≤ 10,
0 ≤ statues[i] ≤ 20.

[output] integer

需要添加到现有雕像的最小数量的雕像,使其包含区间[L,R](对于某些L,R)的每个整数大小,而不包含其他大小。

这是我的代码:

function makeArrayConsecutive2(statues) {
 //range the table from min to max
 var rang=statues.sort();
 var some=0;
 //if the table is one element
 if(rang.length-1==0){
  return 0;

 }else{
  //if the table contain more then one element
  for(i=0;i<=rang.length-2;i++){
   //add the deference of two consecutive position -1 
   //to find the number of missing numbers
   some+=(rang[i+1]-rang[i]-1);
 }
  return some;
 }
}

13 个答案:

答案 0 :(得分:3)

除了排序部分外,一切都是正确的。

您已使用sort函数按递增顺序对数组进行排序

var rang = statues.sort();

但是如果sort函数没有提供compare函数,它会在字符串中转换它的元素,然后以unicode顺序对它进行排序。

例如:[2,1,11]将被排序为[1,11,2],这将产生不希望的输出。

正确的方法是

var rang = statues.sort(function (a, b){
    return (a - b) 
});

答案 1 :(得分:0)

排序(登录)不是必需的。以下是Java中的解决方案。

int makeArrayConsecutive2(int[] statues) {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < statues.length; i++) {
            max = Math.max(max, statues[i]);
            min = Math.min(min, statues[i]);
        }
        return (max - min) + 1 - statues.length;
}

答案 2 :(得分:0)

我同意Deepak的解决方案。现在的问题不是要排序,而是要找出所需的最少额外雕像数量。您只需要获取最大值和最小值。

int makeArrayConsecutive2(int[] statues) 
{
    int min=Integer.MAX_VALUE,max=-1;
    for(int i=0;i<statues.length;i++)
    {
        if(statues[i] < min){ min = statues[i]; }
        if(statues[i] > max){ max = statues[i]; }
    }
     return (max-min)+1 - statues.length;
}

答案 3 :(得分:0)

打字稿中的解决方案。使用for循环从状态数组的最小值和最大值创建一个新数组。用状态数组长度减去新数组长度。

function makeArrayConsecutive2(statues: number[]): number {

    let min = Math.min(...statues);
    let max = Math.max(...statues);
    let arr = [];

    for (let i = min; i <= max; i++) {
        arr.push(i);
    }

    return arr.length - statues.length;
}

答案 4 :(得分:0)

此代码有效

var statues = [2, 3, 6, 8];  
var newStatues = [];

函数声明

function makeArrayConsecutive2(statues) {
    statues.sort(function(a, b) { return a - b });
    for(var i = statues[0]; i <= statues[statues.length-1]; i++) {
        newStatues.push(i);
    }
    return console.log(newStatues.length - statues.length);
}

函数调用

makeArrayConsecutive2(statues);

答案 5 :(得分:0)

最好的解决方案只是O(1)复杂度:

let n = statues.length;
let max = Math.max.apply(null, statues);
let min = Math.min.apply(null, statues);
return max - min - n + 1;

答案 6 :(得分:0)

因此解决此问题的逻辑是:

  1. 查找数组中最小和最大的元素。

  2. 获得可以说的计数,数组的最大和最小值之差才能进行计算,必须有多少个元素才能使其成为连续数组 。像从5到9,总元素数必须为5(即5、6、7、8、9),并且还在结果中加1 以使计数包括在内

  3. 查找数组的长度

  4. 减去计数,即“最大和最小值之差”与数组长度

PYTHON CODE(用于解释):

def makeArrayConsecutive2(statues):
    max_height = max(statues)
    min_height = min(statues)
    array_length  = len(statues)

    count_of_element = max_height - min_height + 1
    # '1 ' is added to make it inclusive
  
    return count_of_element-array_length

和Python一线:

def makeArrayConsecutive2(statues):
    return max(statues)-min(statues)-len(statues)+1

答案 7 :(得分:0)

function makeArrayConsecutive2(statues) {
    var rang = statues.sort(function (a, b){
        return (a - b) 
    });
    var some=0;
 
    if(rang.length-1==0){
        return 0;
    }else{
    for(i=0;i<=rang.length-2;i++){
        some+=(rang[i+1]-rang[i]-1);
    }
    return some;
    }
}

答案 8 :(得分:0)

function makeArrayConsecutive2(statues) {
  const n = statues.length;
  const min = Math.min(...statues);
  const max = Math.max(...statues);
  return max - min - n + 1;
}

如果从最大元素中减去最小值,那么我们将获得最终数组中应包含的元素数量。现在从该数量中减去已经存在的元素数量,然后加1,然后得到所需的结果-缺少元素的数量

答案 9 :(得分:0)

只是为了玩 C#

static int makeArrayConsecutive2(int[] statues)
        {
            List<int> ConsecutiveNums = new List<int>();
            for(int i = statues.Min(); i != statues.Max() + 1; i++)
                ConsecutiveNums.Add(i);           
            return ConsecutiveNums.Count - statues.Length;         
        }

答案 10 :(得分:0)

function makeArrayConsecutive2(statues) {
    return Math.max(...statues) - Math.min(...statues) + 1 -(statues.length) 
}

我认为我们不需要在那里循环,这是我的解决方案

答案 11 :(得分:0)

function makeArrayConsecutive2(statues) {
  const nums = [];

  for (let i = Math.min(...statues); i <= Math.max(...statues); i++) {
    if (!statues.includes(i)) {
      nums.push(i);
    }
  }

  return nums.length;
}

console.log(makeArrayConsecutive2([6, 2, 3, 8]))

答案 12 :(得分:0)

您可以通过以下代码尝试使用for循环和三元运算

def makeArrayConsecutive2(statues):
count=0
for i in range (min(statues),max(statues)):
    count=count+1 if i not in statues else count
return count