目录
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为队列的最大元素个数
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)