基于另一个填充有“<”和“>”符号的数组排列数组

时间:2015-09-30 17:59:15

标签: javascript arrays algorithm

我有两个数组。一个大小为n的数组填充了<>符号。第二个数组的大小为n+1并且具有整数。我必须以这样的方式排列第二个数组,以便它能满足第一个数组的条件。请注意,第一个数组不能被修改。

conditionArray = ['>', '<', '<', '<', '>'];

numArray = [12, 9, 1, 11, 13, 2];

可能的输出:

[9, 1, 11, 12, 13, 2]

2 个答案:

答案 0 :(得分:1)

这是一个想法:

1)对数据使用内置排序方法。这满足conditionArray = ['<', '<', '<' ...]

2)循环通过conditionArray并为每个>获取最后一个元素(最大元素)并将其插入到当前索引中,移动每个其他元素。否则,<已经满足,可以跳过。

在您的示例中,数组将经历以下中间状态:

12, 9, 1, 11, 13, 2 //start

1, 2, 9, 11, 12, 13 // initial sort

13, 1, 2, 8, 11, 12 // index 0 of condition is a > so insert the last element at index 0
13, 1, 2, 8, 11, 12 // < is NOP
13, 1, 2, 8, 11, 12 // < is NOP
13, 1, 2, 8, 11, 12 // < is NOP 
13, 1, 2, 8, 12, 11 // index 4 of condition is a > so insert the last element at index 4

一个非常基本的实现:

conditionArray = ['>', '<', '<', '<', '>'];
numArray = [12, 9, 1, 11, 13, 2];

numArray.sort( function(a, b) {return a -b ; /*lazy number comparison*/ });
console.log(numArray);
var size = numArray.length;
conditionArray.forEach( function(comparison, index) {
    //ignore '<'
    if (comparison == '>')
    {
        var finalElement = numArray[size - 1];
        numArray.splice(index, 0, finalElement); //insert the element
    }
    console.log(numArray);
        
});

numArray = numArray.slice(0, size); //remove all the extra stuff at the end

console.log(numArray);

此代码使用插入很多。它可以很好地处理链接列表,因为插入是一个恒定的时间操作,但它不能很好地扩展到数组。如果您的插入计算量很大(如典型的数组),则可以只在辅助数组中一次插入一个元素。

conditionArray = ['>', '<', '<', '<', '>'];
numArray = [12, 9, 1, 11, 13, 2];

numArray.sort( function(a, b) {return a - b; /*lazy number comparison*/ });
console.log(numArray);
var size = numArray.length;
var newArray = [];
var numInsertedFromBack = 0;

conditionArray.forEach( function(comparison, index) {
    if (comparison == '>')
    {
        var biggestElementNotAlreadyInserted = numArray[size - 1 - numInsertedFromBack];
        newArray.push(biggestElementNotAlreadyInserted);
        numInsertedFromBack += 1;
    }
    else {
        //just insert the next element
        //since we inserted stuff out of turn, we need 
        //to account for each skip, and backtrack that many times
        var smallestElementNotAlreadyInserted = numArray[index - numInsertedFromBack];
        newArray.push(smallestElementNotAlreadyInserted);
    }
    console.log(newArray);
});

//need to manually add the straggling final element
//since the comparisons are of array.length - 1
newArray.push(numArray[size - 1 - numInsertedFromBack]);

console.log(newArray);

答案 1 :(得分:0)

conditionArray = ['>', '<', '<', '<', '>'];
numArray = [12, 9, 1, 11, 13, 2];
isComplete = false;
console.log(numArray);
 while (isComplete == false) {
  isComplete = true;
  for (i = 0; i < conditionArray.length; i++) {
   if ((conditionArray[i] == ">" && numArray[i] < numArray[i+1]) || (conditionArray[i] == "<" && numArray[i] > numArray[i+1])){
        isComplete = false;
        temp = numArray[i];
        numArray[i] = numArray[i+1];
        numArray[i+1] = temp;
      }
    }
  }
 document.write(numArray);
<!DOCTYPE html>
<html lan="en">
  <meta charset="utf-8">
  <body>
  </body>
</html>