C++ two queue solution, compare the index of R and D.


  • 0
    J
    class Solution {
    public:
        string predictPartyVictory(string senate) {
            queue<int> R, D;
            for(int i = 0;i<senate.size();i++)
                senate[i]=='R'?R.push(i):D.push(i);
            
            while(!R.empty() && !D.empty())
            {
                int r_idx = R.front();R.pop();
                int d_idx = D.front();D.pop();
                if(r_idx<d_idx) R.push(r_idx+senate.size());
                else D.push(d_idx+senate.size());
            }
            return R.size()>D.size()?"Radiant":"Dire";   
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.