[Queue, Medium] Dota2 Senate

송재호·2025년 3월 14일
0

https://leetcode.com/problems/dota2-senate/description/?envType=study-plan-v2&envId=leetcode-75

처음에 문제를 잘못 이해했었다.

중요한건 순서대로 권리 행사가 가능하다는 것이며
라운드마다 권리를 잃은 senate는 제외된다는 것이다.

그렇다는 것은 두 값이 다를 때 인덱스가 낮은 쪽이 승리하고 다음 라운드로 넘어간다는 것

한 번 뽑은 인덱스를 그대로 다시 넣어주면 다음 라운드라는 의미가 없어진다.
라운드에서 이긴 senate는 다음 라운드에서 후순위를 가져야 한다.

그래서 다음 라운드 인덱스라는 것을 의미하기 위해 offer 시에는 + senate.length() 해준다.

class Solution {
    public String predictPartyVictory(String senate) {
        
        Queue<Integer> radiant = new LinkedList<>();
        Queue<Integer> dire = new LinkedList<>();
        
        for (int i=0; i<senate.length(); i++) {
            if (senate.charAt(i) == 'R') {
                radiant.offer(i);
            } else {
                dire.offer(i);
            }
        }

        while (!radiant.isEmpty() && !dire.isEmpty()) {
            int r = radiant.poll();
            int d = dire.poll();

            if (r < d) {
                radiant.offer(r + senate.length());
            } else {
                dire.offer(d + senate.length());
            }
        }

        return dire.isEmpty() ? "Radiant" : "Dire";
    }
}
profile
식지 않는 감자

0개의 댓글