leetcode 算法题(队列):最近的请求次数、亲密字符串、设计循环双端队列、 柠檬水找零

379 阅读1分钟

933. 最近的请求次数

image.png

解题思路
  • 小于 t-3000 的请求 都是不符合要求的请求
  • 在循环中,都需要shift掉
var RecentCounter = function() {
    this.queue =[];
};

/** 
 * @param {number} t
 * @return {number}
 */
RecentCounter.prototype.ping = function(t) {
 
    this.queue.push(t);

    while(this.queue[0] < t - 3000) {
        this.queue.shift();
    }

    return this.queue.length;
};

/**
 * Your RecentCounter object will be instantiated and called as such:
 * var obj = new RecentCounter()
 * var param_1 = obj.ping(t)
 */
解法二
var RecentCounter = function() {
    this.queue = [];
    this.cur = 0;
};

/** 
 * @param {number} t
 * @return {number}
 */
RecentCounter.prototype.ping = function(t) {
 
    this.queue.push(t);

    while(this.queue[this.cur] < t - 3000) {
        this.cur++
    }

    return this.queue.length - this.cur;
};

/**
 * Your RecentCounter object will be instantiated and called as such:
 * var obj = new RecentCounter()
 * var param_1 = obj.ping(t)
 */

859. 亲密字符串

image.png

/**
 * @param {string} s
 * @param {string} goal
 * @return {boolean}
 */
var buddyStrings = function(s, goal) {
    let queue = [];
    if(s?.length !== goal?.length) {
        return false;
    }
    // 'ab' 'ab'
    if(s === goal) {
        return s.length > new Set(s).size;
    }

    for(let i=0; i < s.length; i++) {
        if(s[i] !== goal[i] ) {
           queue.push(s[i],goal[i]);
        }
    }
    // 如果大于说明不止两个数字不相等
    if(queue.length > 4) {
        return false;
    }
    // ['a','b','b','a']  两两对比是否相等
   if(queue[0] === queue[3] && queue[1] === queue[2]) {
       return true;
   } else {
       return false;
   }
};

641. 设计循环双端队列

image.png

/**
 * @param {number} k
 */
var MyCircularDeque = function(k) {
    this.queue = new Array(k)
    this.max = k;
    this.count = 0;
    this.head = 0;
    this.tail = 1;
};

/** 
 * @param {number} value
 * @return {boolean}
 */
MyCircularDeque.prototype.insertFront = function(value) {
    if(this.isFull()) {
        return false;
    }
    this.queue[this.head] = value;
    // 循环指针
    this.head = (this.head -1 +this.max)%this.max;
    this.count++;
    return true;
};

/** 
 * @param {number} value
 * @return {boolean}
 */
MyCircularDeque.prototype.insertLast = function(value) {
    if(this.isFull()) {
        return false;
    }
    this.queue[this.tail] = value;
    // 循环指针
    this.tail = (this.tail + 1) % this.max;
    this.count++;
    return true;
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.deleteFront = function() {
    if(this.isEmpty()) {
        return false;
    }
     // 循环指针
    this.head = (this.head + 1) % this.max;
    this.count--;
    return true;

};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.deleteLast = function() {
    if(this.isEmpty()) {
        return false;
    }
     // 循环指针        
    this.tail = (this.tail -1 + this.max)% this.max;
    this.count--;
    return true;
};

/**
 * @return {number}
 */
MyCircularDeque.prototype.getFront = function() {
    if(this.isEmpty()) {
        return -1;
    }
  
    return this.queue[(this.head + 1) % this.max];
};

/**
 * @return {number}
 */
MyCircularDeque.prototype.getRear = function() {
    if(this.isEmpty()) {
        return -1;
    }
  
    return this.queue[(this.tail -1 + this.max)% this.max];  
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.isEmpty = function() {
   return this.count === 0;
};

/**
 * @return {boolean}
 */
MyCircularDeque.prototype.isFull = function() {
   
    return this.count === this.max;
};

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * var obj = new MyCircularDeque(k)
 * var param_1 = obj.insertFront(value)
 * var param_2 = obj.insertLast(value)
 * var param_3 = obj.deleteFront()
 * var param_4 = obj.deleteLast()
 * var param_5 = obj.getFront()
 * var param_6 = obj.getRear()
 * var param_7 = obj.isEmpty()
 * var param_8 = obj.isFull()
 */

柠檬水找零

image.png

/**
 * @param {number[]} bills
 * @return {boolean}
 */
var lemonadeChange = function(bills) {
    let result = true;
    const billMap = new Map();
    billMap.set(5,0)
    billMap.set(10,0)
    billMap.set(20,0)

    const handleTrade = (bill) => {
        let num = billMap.get(bill) ||  0;
         
        switch(bill) {
            case 5:       
            billMap.set(5, num + 1 );
            break;
            case 10:
            if(billMap.get(5) <= 0) {
                return false
            }
            billMap.set(10, num  + 1);
            billMap.set(5, billMap.get(5) - 1);
            break;
            case 20:
            if(billMap.get(10) >= 1) {
                if(billMap.get(5) < 1) {
                    return false;
                }
                billMap.set(10,billMap.get(10) - 1)
                billMap.set(5, billMap.get(5) - 1)
                billMap.set(20, num + 1);
            } else {
                if(billMap.get(5) < 3){
                    return false;
                }
                billMap.set(5, billMap.get(5) - 3);
                billMap.set(20, num + 1);
            }
            break;
        }
    }
    for(let i = 0;i < bills.length; i++) {
        const bill = bills[i];
        if(handleTrade(bill) === false) {
            return false;
        }
    }
    
    return result;
};