1 Star 0 Fork 0

ccyy-阿亮/datastructures_ts

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hot-potato.ts 728 Bytes
一键复制 编辑 原始数据 按行查看 历史
import Queue from '../data-structures/queue';
/**
* 用队列模拟击鼓传花
* @param elementsList 那些人
* @param num 到第几次淘汰
*/
export function hotPotato(elementsList: any[], num: number) {
const queue = new Queue();
const elimitatedList = [];
for (let i = 0, el = elementsList.length; i < el; i++) {
queue.enqueue(elementsList[i]);
}
while (queue.size() > 1) {
for (let i = 0; i < num; i++) { // 在num次后淘汰一人
queue.enqueue(queue.dequeue()); // 从首端取出放入尾端
}
elimitatedList.push(queue.dequeue());
}
return {
elimitated: elimitatedList, // 淘汰顺序
winner: queue.dequeue(),
};
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/liawnliu/datastructures_ts.git
git@gitee.com:liawnliu/datastructures_ts.git
liawnliu
datastructures_ts
datastructures_ts
master

搜索帮助