LeetCode75| 队列

目录

[933 最近的请求次数](#933 最近的请求次数)

[649 Dota2 参议院](#649 Dota2 参议院)


933 最近的请求次数

cpp 复制代码
class RecentCounter {
public:
    queue<int>st;
    RecentCounter() {

    }
    
    int ping(int t) {
        st.push(t);
        while(t - st.front() > 3000)st.pop();
        return st.size();
    }
};

时间复杂度O(1)

空间复杂度O(n)//n为队列的最大元素个数

649 Dota2 参议院

cpp 复制代码
class Solution {
public:
    string predictPartyVictory(string senate) {
        int n = senate.size();
        queue<int>R,D;
        for(int i = 0;i < n;i++){
            char ch = senate[i];
            if(ch == 'R'){
                R.push(i);
            }else{
                D.push(i);
            }
        }
        while(!R.empty() && !D.empty()){
            if(R.front() < D.front()){
                R.push(R.front() + n);
            }else{
                D.push(D.front() + n);
            }
            D.pop();
            R.pop();
        }
        if(R.empty())return "Dire";
        return "Radiant"; 
    }
};

时间复杂度O(n)

空间复杂度O(n)

相关推荐
漫随流水1 天前
leetcode算法(344.反转字符串)
数据结构·算法·leetcode
sin_hielo1 天前
leetcode 1411(递推)
数据结构·算法·leetcode
颜酱1 天前
学习卡特兰数:从原理到应用,解决所有递推计数问题
前端·javascript·算法
漫随流水1 天前
leetcode算法(20.有效的括号)
数据结构·算法·leetcode
如果你想拥有什么先让自己配得上拥有1 天前
数、自然数、整数、有理数、无理数它们的定义由来和边界划分
算法
Xの哲學1 天前
Linux SLUB 内存分配器深度剖析: 从设计哲学到实战调试
linux·服务器·网络·算法·边缘计算
2401_876221341 天前
AtCoder Beginner Contest 439 - D - Kadomatsu Subsequence
c++·算法
小丁努力不焦虑1 天前
算法期末总结题
数据结构·算法
嵌入式进阶行者1 天前
【算法】从数组中选取两个符合一定条件的数的算法与实例:华为OD机考双机位A卷 - 跳房子I
数据结构·c++·算法·链表
老歌老听老掉牙1 天前
从战场到商场:最优化算法如何用数学重塑世界?
python·算法·最优化