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 make a decision about 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 make the decision about the change in the game.




Given a string representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and the Dire party respectively. 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, you need to predict which party will finally announce the victory and make the change in the Dota2 game. The output should be Radiant or Dire.


Example 1:

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




Example 2:

Input: "RDD"
Output: "Dire"
Explanation: 
The first senator comes from Radiant and he can just ban the next senator's right in the 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 the round 1. And in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.



Note:

The length of the given string will in the range [1, 10,000].

Difficulty: MEDIUM_HARD
Tags: String algorithms

Data structures

Greedy algorithms
Skill types: Data structures

Greedy algorithms

Starter code:
class Solution:
    def predictPartyVictory(self, senate: str) -> str:
        

Solutions:
class Solution:

	def predictPartyVictory(self, senate):
		num = 0
		while 'R' in senate and 'D' in senate:
			res = []
			for i in senate:
				if i == 'R':
					if num >= 0:
						res.append(i)
					num += 1
				else:
					if num <= 0:
						res.append(i)
					num -= 1
			senate = res
		return 'Radiant' if 'R' in senate else 'Dire'


class Solution:

	def predictPartyVictory(self, senate):
		from collections import deque
		n = len(senate)
		queueR = deque()
		queueD = deque()
		for i in range(len(senate)):
			if senate[i] == 'R':
				queueR.append(i)
			else:
				queueD.append(i)
		while queueR and queueD:
			p1 = queueR.popleft()
			p2 = queueD.popleft()
			if p1 < p2:
				queueR.append(p1 + n)
			else:
				queueD.append(p2 + n)
		if queueR:
			return 'Radiant'
		return 'Dire'


class Solution:

	def predictPartyVictory(self, senate):
		r = collections.deque()
		d = collections.deque()
		for (i, c) in enumerate(senate):
			if c == 'R':
				r.append(i)
			else:
				d.append(i)
		return self.eliminateSenators(r, d)

	def eliminateSenators(self, r, d):
		if len(r) == 0:
			return 'Dire'
		if len(d) == 0:
			return 'Radiant'
		r2 = collections.deque()
		d2 = collections.deque()
		while len(r) > 0 or len(d) > 0:
			if len(r) == 0:
				if len(r2) == 0:
					return 'Dire'
				else:
					r2.popleft()
					d2.append(d.popleft())
			elif len(d) == 0:
				if len(d2) == 0:
					return 'Radiant'
				else:
					d2.popleft()
					r2.append(r.popleft())
			else:
				r_curr = r.popleft()
				d_curr = d.popleft()
				if r_curr < d_curr:
					r2.append(r_curr)
				else:
					d2.append(d_curr)
		return self.eliminateSenators(r2, d2)


class Solution:

	def predictPartyVictory(self, senate):
		queue = collections.deque()
		(people, bans) = ([0, 0], [0, 0])
		for person in senate:
			x = person == 'R'
			people[x] += 1
			queue.append(x)
		while all(people):
			x = queue.popleft()
			if bans[x]:
				bans[x] -= 1
				people[x] -= 1
			else:
				bans[x ^ 1] += 1
				queue.append(x)
		return 'Radiant' if people[1] else 'Dire'


class Solution:

	def predictPartyVictory(self, senate):
		s = [1 if x == 'R' else 0 for x in senate]
		Rban = 0
		Dban = 0
		i = 0
		ban = False
		while True:
			if s[i] == -1:
				pass
			elif s[i] == 1:
				if Dban > 0:
					Dban -= 1
					s[i] = -1
					ban = True
				else:
					Rban += 1
			elif Rban > 0:
				Rban -= 1
				s[i] = -1
				ban = True
			else:
				Dban += 1
			i += 1
			if i == len(s):
				i = 0
				if not ban:
					break
				else:
					ban = False
		if Rban > 0:
			return 'Radiant'
		else:
			return 'Dire'