Java Solution using TreeSet


  • 0
    F
        public String predictPartyVictory(String senate) {
            TreeSet<Integer> rSet = new TreeSet<Integer> ();
            TreeSet<Integer> dSet = new TreeSet<Integer> ();
    
            for(int i=0; i<senate.length(); i++) {
                if(senate.charAt(i) == 'R') rSet.add(i);
                else dSet.add(i);
            }
            
            while(!rSet.isEmpty() && !dSet.isEmpty()) {
                int rFirst = rSet.pollFirst();
                int dFirst = dSet.pollFirst();
                if(rFirst > dFirst) {
                    dSet.add(dFirst + senate.length());                
                } else {
                    rSet.add(rFirst + senate.length());                
                }
            }
            
            return rSet.isEmpty()? "Dire" : "Radiant";
        }
    

Log in to reply
 

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