- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum Length of Pair Chain in C++
Suppose in the world of Dota2, there are two parties − The Radiant and also the Dire. The Dota2 senate consists of senators coming from two parties. Now the senate wants to form a choice a few change within the Dota2 game. The voting for this alteration may be a round-based procedure. In each round, each senator can exercise one among the two rights −
Ban one senator's right − A senator can make another senator lose all his rights during this and every one the subsequent rounds.
Announce the victory − If this senator found the senators who still have rights to vote are all from an equivalent party, he can announce the victory and make the choice about the change within the game.
Given a string representing each senator's party belonging. The character 'R' and 'D' represent the Radiant party and also the Dire party respectively. Then if there are n senators, the dimensions of the given string are going to be n.
The round-based procedure starts from the primary senator to the last senator within the given order. This procedure will last until the top of voting. All the senators who have lost their rights are going to be skipped during the procedure.
Suppose every senator is sensible enough and can play the simplest strategy for his own party, you would like to predict which party will finally announce the victory and make the change within the Dota2 game. The output should be Radiant or Dire.
So if the input is like “RDD”, it will be Dire. This is because 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. After that the third senator comes from Dire and he can ban the first senator's right in the round 1. Now in the round 2, the third senator can just announce the victory since he is the only guy in the senate who can vote.
To solve this, we will follow these steps −
Make a queue q1, q2, n := size of string. For all R insert into q1, and for all D, insert into q2.
while both queues are not empty
if front element of q1 < front element of q2, then
insert n into q1, delete from q2 and q1
otherwise insert n into q2, delete from q2 and q1
increase n by 1
if queue is empty, then return Dire, otherwise, return Radiant
Example(C++)
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: string predictPartyVictory(string s) { queue <int> q1, q2; int n = s.size(); for(int i = 0; i < s.size(); i++){ if(s[i] == 'R'){ q1.push(i); } else { q2.push(i); } } while(q1.size() && q2.size()){ if(q1.front() < q2.front()){ q1.push(n); q2.pop(); q1.pop(); } else { q2.push(n); q2.pop(); q1.pop(); } n++; } return q1.empty()? "Dire" : "Radiant"; } }; main(){ Solution ob; cout <<(ob.predictPartyVictory("RDD")); }
Input
"RDD"
Output
Dire
- Related Articles
- Maximum Length Chain of Pairs in C++
- Maximum Length Chain of Pairs
- Probability of a random pair being the maximum weighted pair in C++
- Length of longest string chain in JavaScript
- Word Ladder (Length of shortest chain to reach a target word) in C++
- Maximum XOR value of a pair from a range in C++
- Count ways of choosing a pair with maximum difference in C++
- Find a pair with maximum product in array of Integers in C++
- Maximum bitwise AND value of a pair in an array in C++
- Maximum Bitwise AND pair from given range in C++
- Maximum length of rod for Qth person in C++
- Find maximum average subarray of k length in C++
- Find pair with maximum GCD in an array in C++
- Find Sum of pair from two arrays with maximum sum in C++
- Find maximum length Snake sequence in C++
