[leetcode] 649. Dota2 Senate

网友投稿 675 2022-11-08

[leetcode] 649. Dota2 Senate

[leetcode] 649. Dota2 Senate

Description

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].

分析

题目的意思是:这道题模拟了刀塔类游戏开始之前的BP过程,两个阵营按顺序Ban掉对方的英雄,看最后谁剩下来了,就返回哪个阵营。

dota没有玩过,所以这个有点难办。不过仔细看了一下解法,发现并不难,如果能想到用(i+n)%j的方式处理循环的话,就不是难题了。

代码

class Solution {public: string predictPartyVictory(string senate) { int n=senate.size(); int cntR=0; int cntD=0; for(char c:senate){ c=='R' ? cntR++:cntD++; } if(cntR==0) return "Dire"; if(cntD==0) return "Radiant"; while(cntR>0&&cntD>0){ for(int i=0;i

参考文献

​​[LeetCode] Dota2 Senate 刀塔二参议院​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:[leetcode] 525. Contiguous Array
下一篇:[leetcode] 744. Find Smallest Letter Greater Than Target
相关文章

 发表评论

暂时没有评论,来抢沙发吧~