Skip to content

Commit a26f805

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 649
1 parent 4fe5c13 commit a26f805

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
- [637 Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/)
244244
- [643 Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/description/)
245245
- [646 Maximum Length of Pair Chain](https://leetcode.com/problems/maximum-length-of-pair-chain/description/)
246+
- [649 Dota2 Senate](https://leetcode.com/problems/dota2-senate/description/)
246247
- [673 Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/)
247248
- [712 Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/)
248249
- [714 Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def predictPartyVictory(self, senate: str) -> str:
5+
"""
6+
In the world of Dota2, there are two parties: the Radiant and the Dire.
7+
8+
The Dota2 senate consists of senators coming from two parties. Now the Senate
9+
wants to decide on a change in the Dota2 game. The voting for this change is a
10+
round-based procedure. In each round, each senator can exercise one of the two
11+
rights:
12+
- Ban one senator's right: A senator can make another senator lose all his
13+
rights in this and all the following rounds.
14+
- Announce the victory: If this senator found the senators who still have
15+
rights to vote are all from the same party, he can announce the victory and
16+
decide on the change in the game.
17+
18+
Given a string senate representing each senator's party belonging. The
19+
character 'R' and 'D' represent the Radiant party and the Dire party. Then if
20+
there are n senators, the size of the given string will be n.
21+
22+
The round-based procedure starts from the first senator to the last senator in
23+
the given order. This procedure will last until the end of voting. All the
24+
senators who have lost their rights will be skipped during the procedure.
25+
26+
Suppose every senator is smart enough and will play the best strategy for his
27+
own party. Predict which party will finally announce the victory and change the
28+
Dota2 game. The output should be "Radiant" or "Dire".
29+
"""
30+
queue = list(senate)
31+
while len(queue) > 1:
32+
senator = queue.pop(0)
33+
another = "D" if senator == "R" else "R"
34+
try:
35+
index = queue.index(another)
36+
except ValueError:
37+
break
38+
queue.pop(index)
39+
queue.append(senator)
40+
return "Radiant" if queue.pop(0) == "R" else "Dire"

tests/test_649_dota2_senate.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._649_dota2_senate import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["senate", "expected"],
8+
argvalues=[
9+
("RD", "Radiant"),
10+
("RDD", "Dire"),
11+
("DDR", "Dire"),
12+
],
13+
)
14+
def test_func(senate: str, expected: str):
15+
"""Tests the solution of a LeetCode problem."""
16+
senator = Solution().predictPartyVictory(senate)
17+
assert senator == expected

0 commit comments

Comments
 (0)