输入: 1,2,2
输出:2,0,1
说明:队列头部任务的优先级为1,被移到队列尾部;接着顺序打印两个优先级为2的任务,故其序号分别为0和1;最后打印剩下的优先级为1的任务,其序号为2
解法:
const str = "9,3,5";
const str1 = "1,2,2";
function solution(str) {
const arr = str.split(",");
const queen = arr.map((item, index) => {
return {
value: item,
index: index,
};
});
const recordList = [];
let outIndex = 0;
while (queen.length) {
let { value, index } = queen.shift();
if (value >= Math.max(...queen.map((item) => item.value))) {
recordList.push({
value,
outIndex: outIndex++,
index,
});
} else {
queen.push({
value,
index,
});
}
}
return recordList
.sort((a, b) => a.index - b.index)
.map((item) => item.outIndex)
.join(",");
}
console.log(solution(str1));
/*
9,3,5 = > 0,2,1
1,2,2 => 2,0,1
*/