Problem

In the world of Dota2, there are two parties: the Radiant and the Dire.

The Dota2 senate consists of senators coming from two parties. Now the Senate wants to decide on a change in the Dota2 game. The voting for this change is a round-based procedure. In each round, each senator can exercise one of the two rights:

  • Ban one senator ’s right: A senator can make another senator lose all his rights in this and all the following rounds.
  • Announce the victory: If this senator found the senators who still have rights to vote are all from the same party, he can announce the victory and decide on the change in the game.

Given a string senate representing each senator’s party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party. Then if there are n senators, the size of the given string will be n.

The round-based procedure starts from the first senator to the last senator in the given order. This procedure will last until the end of voting. All the senators who have lost their rights will be skipped during the procedure.

Suppose every senator is smart enough and will play the best strategy for his own party. Predict which party will finally announce the victory and change the Dota2 game. The output should be "Radiant" or "Dire".

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

    
    
    Input: senate = "RD"
    Output: "Radiant"
    Explanation: 
    The first senator comes from Radiant and he can just ban the next senator's right in round 1. 
    And the second senator can't exercise any rights anymore since his right has been banned. 
    And in round 2, the first senator can just announce the victory since he is the only guy in the senate who can vote.
    

Example 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

    
    
    Input: senate = "RDD"
    Output: "Dire"
    Explanation: 
    The first senator comes from Radiant and he can just ban the next senator's right in round 1. 
    And the second senator can't exercise any rights anymore since his right has been banned. 
    And the third senator comes from Dire and he can ban the first senator's right in round 1. 
    And in round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
    

Constraints

  • n == senate.length
  • 1 <= n <= 10^4
  • senate[i] is either 'R' or 'D'.

Solution

Method 1 – Greedy with Queues

Intuition

We can use two queues to track the indices of Radiant and Dire senators. In each round, the senator with the smaller index acts first and bans the other. The acting senator is re-enqueued with their index increased by n (to simulate the next round). The process continues until one queue is empty, and the remaining party wins.

Approach

  1. Initialize two queues for indices of ‘R’ and ‘D’ senators.
  2. For each round, compare the front indices of both queues:
    • The senator with the smaller index bans the other and is re-enqueued with index + n.
    • Remove the banned senator from their queue.
  3. Repeat until one queue is empty.
  4. Return the winning party.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
    string predictPartyVictory(string senate) {
        queue<int> r, d;
        int n = senate.size();
        for (int i = 0; i < n; ++i) {
            if (senate[i] == 'R') r.push(i);
            else d.push(i);
        }
        while (!r.empty() && !d.empty()) {
            if (r.front() < d.front()) {
                r.push(r.front() + n);
            } else {
                d.push(d.front() + n);
            }
            r.pop(); d.pop();
        }
        return r.empty() ? "Dire" : "Radiant";
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func predictPartyVictory(senate string) string {
    n := len(senate)
    r, d := []int{}, []int{}
    for i, c := range senate {
        if c == 'R' {
            r = append(r, i)
        } else {
            d = append(d, i)
        }
    }
    for len(r) > 0 && len(d) > 0 {
        if r[0] < d[0] {
            r = append(r, r[0]+n)
        } else {
            d = append(d, d[0]+n)
        }
        r = r[1:]
        d = d[1:]
    }
    if len(r) > 0 {
        return "Radiant"
    }
    return "Dire"
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    public String predictPartyVictory(String senate) {
        Queue<Integer> r = new LinkedList<>(), d = new LinkedList<>();
        int n = senate.length();
        for (int i = 0; i < n; ++i) {
            if (senate.charAt(i) == 'R') r.offer(i);
            else d.offer(i);
        }
        while (!r.isEmpty() && !d.isEmpty()) {
            if (r.peek() < d.peek()) r.offer(r.poll() + n);
            else d.offer(d.poll() + n);
            r.poll(); d.poll();
        }
        return r.isEmpty() ? "Dire" : "Radiant";
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    fun predictPartyVictory(senate: String): String {
        val n = senate.length
        val r = ArrayDeque<Int>()
        val d = ArrayDeque<Int>()
        for (i in senate.indices) {
            if (senate[i] == 'R') r.add(i) else d.add(i)
        }
        while (r.isNotEmpty() && d.isNotEmpty()) {
            if (r.first() < d.first()) r.add(r.removeFirst() + n)
            else d.add(d.removeFirst() + n)
            r.removeFirst(); d.removeFirst()
        }
        return if (r.isNotEmpty()) "Radiant" else "Dire"
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class Solution:
    def predictPartyVictory(self, senate: str) -> str:
        from collections import deque
        n = len(senate)
        r = deque()
        d = deque()
        for i, c in enumerate(senate):
            if c == 'R':
                r.append(i)
            else:
                d.append(i)
        while r and d:
            if r[0] < d[0]:
                r.append(r[0] + n)
            else:
                d.append(d[0] + n)
            r.popleft()
            d.popleft()
        return "Radiant" if r else "Dire"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
impl Solution {
    pub fn predict_party_victory(senate: String) -> String {
        use std::collections::VecDeque;
        let n = senate.len();
        let mut r = VecDeque::new();
        let mut d = VecDeque::new();
        for (i, c) in senate.chars().enumerate() {
            if c == 'R' {
                r.push_back(i);
            } else {
                d.push_back(i);
            }
        }
        while !r.is_empty() && !d.is_empty() {
            if r[0] < d[0] {
                r.push_back(r[0] + n);
            } else {
                d.push_back(d[0] + n);
            }
            r.pop_front();
            d.pop_front();
        }
        if r.is_empty() { "Dire".to_string() } else { "Radiant".to_string() }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    predictPartyVictory(senate: string): string {
        const n = senate.length;
        const r: number[] = [], d: number[] = [];
        for (let i = 0; i < n; ++i) {
            if (senate[i] === 'R') r.push(i);
            else d.push(i);
        }
        while (r.length && d.length) {
            if (r[0] < d[0]) r.push(r[0] + n);
            else d.push(d[0] + n);
            r.shift(); d.shift();
        }
        return r.length ? "Radiant" : "Dire";
    }
}

Complexity

  • ⏰ Time complexity: O(n), where n is the length of senate. Each senator is processed at most twice per round.
  • 🧺 Space complexity: O(n), for the queues.