1. 程式人生 > 其它 >leetcode 649. Dota2 參議院

leetcode 649. Dota2 參議院

技術標籤:leetcode刷題leetcode

兩個佇列模擬,彷彿麻煩了點。

class Solution {
public:
    string predictPartyVictory(string senate) {
        deque<char> queue;
        deque<char> tmp;
        int D=0,R=0,D_flag=0,R_flag=0;
        for(char &c:senate) queue.push_back(c);

        while(true){
            for
(char &c:queue) { if(c=='D') { D_flag=1; if(D>=0) tmp.push_back('D'); R-=1; D+=1; }else{ R_flag=1; if(R>=0) tmp.push_back('R'); D-
=1; R+=1; } } if(R_flag==0||D_flag==0) break; queue = tmp; tmp.clear(); D_flag=R_flag=0; } return R_flag==0?"Dire":"Radiant"; } };

這樣的效率高一些

class Solution {
public:
    string predictPartyVictory
(string senate) { int n = senate.size(); queue<int> R,D; for(int i=0;i<n;i++) { if(senate[i]=='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); R.pop(); D.pop(); } if(R.empty()) return "Dire"; return "Radiant"; } };