[leetcode]649. Dota2 参议院

在这里插入图片描述
在这里插入图片描述

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";
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值