2020-12-11 Dota2参议院

题目

解题思路

  • 若当前所有议员都是天辉或夜魇的人,那么直接得出结果;
  • 否则,需要从对方还可以发言的议员中选择一位,将其禁掉。选择谁呢?应当优先选择这一轮中还能够发言的第一个对方议员,其次选择下一轮中第一个发言的议员。

实现代码

class Solution {
public:
    string predictPartyVictory(string senate) {
        queue<int> radiant, dire;
        int n = senate.length();
        for (int i = 0; i < n; i++){
            if (senate[i] == 'R')
                radiant.push(i);
            else
                dire.push(i);
        }

        while (!radiant.empty() && !dire.empty()){
            if (radiant.front() < dire.front()){
                radiant.push(n + radiant.front());
            } else {
                dire.push(n + dire.front());
            }
            dire.pop();
            radiant.pop();
        }
        if (dire.empty()){
            return "Radiant";
        } else {
            return "Dire";
        }
    }
};

提交结果

posted @ 2020-12-11 20:43  刷书狂魔  阅读(73)  评论(0编辑  收藏  举报
总访问: counter for blog 次