simple idea, Greedy


  • 0
    B

    just ban the first opponent following you.

       public String predictPartyVictory(String senate) {
            char[] c = senate.toCharArray();
            int noRight = 0, countR = 0, countD = 0;
            while(noRight + countR < c.length && noRight + countD < c.length ){
                for(int i = 0; i < c.length; i++ ){
                    if(c[i] == 'R'){
                        if(countD > 0){
                            countD--;
                            c[i] = 'N';
                        }else{
                            countR++;
                        }
                    }else if(c[i] == 'D'){
                        if(countR > 0){
                            countR--;
                            c[i] = 'N';
                        }else{
                            countD++;
                        }
                    }else noRight++;
                }
                noRight = 0;
            }
            return countR > 0? "Radiant" : "Dire";
        }

Log in to reply
 

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