剑指offer 2.24

57 阅读1分钟

剑指 Offer 59 - II. 队列的最大值

// 我啥时候能不把while写成if就是胜利了。
var MaxQueue = function () {
    this.arr = [];
    this.store = [];
};

/**
 * @return {number}
 */
MaxQueue.prototype.max_value = function () {
    if(!this.arr.length) return -1;
    return this.store[0];
};

/** 
 * @param {number} value
 * @return {void}
 */
MaxQueue.prototype.push_back = function (value) {
    this.arr.push(value);
    while(this.store.length&&this.store[this.store.length-1]<value) this.store.pop();
    this.store.push(value);
};

/**
 * @return {number}
 */
MaxQueue.prototype.pop_front = function () {
    if(!this.arr.length) return -1;
    const tmp = this.arr[0];
    this.arr.shift();
    if(this.store[0]==tmp) this.store.shift();
    return tmp;
};

剑指 Offer 49. 丑数

// 三指针
var nthUglyNumber = function(n) {
    if(n<1) return [];
    let res = [1];
    let two = [];
    let three = [];
    let five = [];
    let i = 0;
    let j = 0;
    let k = 0;
    while(res.length!=n){
        two.push(2*res[res.length-1]);
        three.push(3*res[res.length-1]);
        five.push(5*res[res.length-1]);
        let tmp = Math.min(two[i],three[j],five[k]);
        if(tmp==two[i]) i++;
        if(tmp==three[j]) j++;
        if(tmp==five[k]) k++;
        res.push(tmp);
    }
    return res[n-1];
};
// 最小堆,最小堆写亿遍真的记不住
var nthUglyNumber = function (n) {
    class Heap {
        constructor() {
            this.arr = [0];
            this.cmp = (x, y) => x < y;
        }
        sink() {
            let index = 1;
            while (2 * index < this.arr.length) {
                let left = 2 * index;
                const tmp = this.arr[index];
                if (left + 1 < this.arr.length && this.cmp(this.arr[left + 1], this.arr[left]) && this.cmp(this.arr[left + 1], tmp)) {
                    this.arr[index] = this.arr[left + 1];
                    index = left + 1;
                    this.arr[index] = tmp;
                    continue;
                }
                if (this.cmp(this.arr[left], tmp)) {
                    this.arr[index] = this.arr[left];
                    index = left;
                    this.arr[index] = tmp;
                    continue;
                }
                break;
            }
        }
        swim() {
            let index = this.arr.length - 1;
            while (index > 1 && this.cmp(this.arr[index], this.arr[Math.floor(index / 2)])) {
                const tmp = this.arr[index];
                this.arr[index] = this.arr[Math.floor(index / 2)];
                index = Math.floor(index / 2);
                this.arr[index] = tmp;
            }
        }
        push(val) {
            this.arr.push(val);
            this.swim();
        }
        pop() {
            const tmp = this.arr[1];
            this.arr[1] = this.arr[this.arr.length - 1];
            this.arr.pop();
            this.sink();
            return tmp;
        }
        size() {
            return this.arr.length - 1;
        }
        peak() {
            return this.arr[1];
        }
    }
    let ugly = 0;
    let heap = new Heap();
    heap.push(1);
    let set = new Set();
    set.add(1);
    for (let i = 0; i < n; i++) {
        ugly = heap.pop();
        for(let i of [2,3,5]){
            if(!set.has(i*ugly)){
                set.add(i*ugly);
                heap.push(i*ugly)
            }
        }
    }
    return ugly;
};