class Solution {
public:
string predictPartyVictory(string senate) {
int n = senate.size();
list<int>r_list;
list<int>d_list;
for(int i = 0; i < n; i++)
{
if(senate[i] == 'R')
{
r_list.push_back(i);
}
else
{
d_list.push_back(i);
}
}
unordered_set<int>out;
string res;
bool flag = false;
while(flag == false)
{
for(int i = 0; i < n; i++)
{
if(out.count(i) == 1) continue;
if(senate[i] == 'R')
{
if(d_list.size() == 0)
{
flag = true;
res = "Radiant";
break;
}
else
{
out.insert(d_list.front());
d_list.pop_front();
r_list.push_back(r_list.front()); //R的这个编号的议员对D危险系数不如R方下一个议员
r_list.pop_front();
}
}
else
{
if(r_list.size() == 0)
{
flag = true;
res = "Dire";
break;
}
else
{
out.insert(r_list.front());
r_list.pop_front();
d_list.push_back(d_list.front());
d_list.pop_front();
}
}
}
}
return res;
}
};
参考leetcode官方的
class Solution {
public:
string predictPartyVictory(string senate) {
int n = senate.size();
queue<int>q;
int people[2] = {0, 0};
int bans[2] = {0, 0};
for(int i = 0; i < n; i++)
{
int x = (senate[i] == 'R' ? 1 : 0);
people[x]++;
q.push(x);
}
while(people[0] > 0 && people[1] > 0)
{
int x = q.front(); q.pop();
if(bans[x] > 0)
{//对方阵营有禁令可以禁止本方议员的权利
bans[x]--; //对方阵营禁令数减一
people[x]--; //本方人数-1;
}
else
{
bans[x^1]++; //本方禁令数+1
q.push(x); //进入存活议员队列
}
}
return people[1] > 0? "Radiant" : "Dire";
}
};