zoukankan      html  css  js  c++  java
  • 495 队列,优先级队列

    一、队列

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
    
    </body>
    
    </html>
    <script>
        function Queue() {
            // 创建一个队列的容器
            this.container = [];
        }
        Queue.prototype = {
            constructor: Queue,
            // 进入队列  element进入队列的元素
            enter: function enter(element) {
                this.container.push(element);
            },
            // 移除队列
            leave: function leave() {
                if (this.container.length === 0) return;
                return this.container.shift();
            },
            // 查看队列的长度
            size: function size() {
                return this.container.length;
            },
            // 查看队列的内容
            value: function value() {
                // 深度克隆是为了保证后期外面接收到的结果不论如何的操作都不会影响内部容器中的内容
                // this.container.slice(0) 浅拷贝
                // JSON.parse(JSON.stringify(this.container)) 深拷贝
                return JSON.parse(JSON.stringify(this.container));
            }
        };
    
        // 创建一个队列
        let qe = new Queue();
        qe.enter(1);
        qe.enter(2);
        qe.enter(3);
        qe.enter(4);
        qe.enter(5);
        qe.leave();
        console.log(qe.value()); // [2, 3, 4, 5]
    
    
        // ------------
    
    
        /* 算法面试题:击鼓传花 */
        // n:参加游戏的人数  m:关键数
        function game(n, m) {
            let qe = new Queue();
    
            // 先把人都依次放入到队列中
            for (let i = 1; i <= n; i++) {
                qe.enter(i);
            }
    
            // 开始处理
            while (qe.size() > 1) {
                for (let i = 0; i < m - 1; i++) {
                    qe.enter(qe.leave());
                }
                qe.leave();
            }
    
            return qe.value()[0];
        }
        let res = game(6, 4);
        console.log(res); // 5
    </script>
    

    二、优先级队列

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
    
    <body>
    
    </body>
    
    </html>
    <script>
        function Queue() {
            this.container = [];
        }
    
        Queue.prototype = {
            constructor: Queue,
            // 进入队列  priority优先级,默认都是0,数值越大,优先级越高
            enter: function enter(element, priority = 0) {
                let obj = {
                    value: element,
                    priority: priority
                };
                if (priority === 0) {
                    // 不指定优先级(最小优先级):存储到末尾即可
                    this.container.push(obj);
                    return;
                }
                // 指定优先级,我们需要从最后一项依次来比较
                let flag = false;
                for (let i = this.container.length - 1; i >= 0; i--) {
                    let item = this.container[i];
                    if (item.priority >= priority) {
                        // 插入到比较项的后面,也就是它的下一项的前面 【从 i + 1 开始,删除0项,插入obj】
                        this.container.splice(i + 1, 0, obj);
                        flag = true;
                        break;
                    }
                };
                // 没有比我大的,我就是最大的,插入到容器最开始的位置即可
                !flag ? this.container.unshift(obj) : null;
            },
    
            // 移除队列
            leave: function leave() {
                if (this.container.length === 0) return;
                return this.container.shift();
            },
    
            // 查看队列的长度
            size: function size() {
                return this.container.length;
            },
    
            // 查看队列的内容
            value: function value() {
                return JSON.parse(JSON.stringify(this.container));
            }
        };
    
        let qe = new Queue();
        qe.enter(1);
        qe.enter(2);
        qe.enter(3);
        qe.enter(4);
        qe.enter(5);
        console.log(qe.value());
    
    </script>
    
  • 相关阅读:
    AcWing 524. 愤怒的小鸟
    AcWing 算法提高课题解目录
    AcWing 292. 炮兵阵地
    AcWing 798. 差分矩阵
    golang 写数据到excel文件 清明
    使用golang开发mqtt服务压力测试工具 清明
    Linux云服务器安装JDK步骤 清明
    shell monitor memory 清明
    自己实现一个Electron跨进程消息组件(兼新书自荐)
    如何把Electron做成一个Runtime,让多个应用共享同一个Electron
  • 原文地址:https://www.cnblogs.com/jianjie/p/13219679.html
Copyright © 2011-2022 走看看