Leetcode Daily Problem

649. Dota2 Senate

one of the most important things to understand in this problem is that when A senator can make another senator lose all his rights in this and all the following rounds. then one who kills another senator still has power.

Let's say we have R and D, senator R makes D loses all its powers, then it means D will lose all its powers but not R, R can still use its powers in upcoming rounds.

In this particular example, we can see that when R at i=0 cancels D at i=1 but R will remain there that's why we added R at the end and when D at i=2 cancels R at i=3 again D is added at the end.

string predictPartyVictory(string senate) 
    {
        int score=0;

        for(int i=0;i<senate.size();i++)
        {
            const char ch=senate[i];

            if(ch == 'R')
            {
                if(score<0)
                {
                    senate.push_back('D');
                }

                score++;
            }
            else
            {
                if(score>0)
                {
                    senate.push_back('R');
                }

                score--;
            }
        }

        if(score>0)  return "Radiant";
        }
        else
        return "Dire";

    }