LeetCode75| 队列

目录

933 最近的请求次数

649 Dota2 参议院


933 最近的请求次数

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 参议院

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)

猜你喜欢

转载自blog.csdn.net/m0_72832574/article/details/135276451
今日推荐