Java ArrayList Solution


  • 0

    Well, this solution is a slower than using array. But the idea is straightforward, remove the first opponent behind you and use circular traverse.

    public String predictPartyVictory(String senate) {
        List<Character> senates = new ArrayList<>();
        for (int i = 0; i < senate.length(); i++) {
            senates.add(senate.charAt(i));
        }
        int i = 0;
        while(true) {
            if (senates.get(i)=='R') {
                int k = i+1;
                while(senates.get(k%senates.size())!='D') {
                    k++;
                    if ( k >= i+senates.size()) {
                        return "Radiant";
                    }
                }
                senates.remove(k%senates.size());
            }
            else {
                int k = i+1;
                while(senates.get(k%senates.size())!='R') {
                    k++;
                    if ( k >= i+senates.size()) {
                        return "Dire";
                    }
                }
                senates.remove(k%senates.size());
            }
            i++;
            if (i>=senates.size()) i = 0;
        }
    }

Log in to reply
 

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