[LeetCode] 649. Dota2 Senate

Semidragon·2023년 11월 16일
0

CodingTest

목록 보기
26/80
post-thumbnail

1. Question

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

Example 1:

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:

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.

2. Thoughts

3. Tips learned

4. My solution

class Solution:
    def predictPartyVictory(self, senate: str) -> str:
        radiant, dire = deque(), deque()
        for i, s in enumerate(senate):
            if s == "R":
                radiant.append(i)
            else:
                dire.append(i)
        senate_len = len(senate)

        while radiant and dire:
            if radiant[0] > dire[0]:
                dire.append(dire[0] + senate_len)
            else:
                radiant.append(radiant[0] + senate_len)
            radiant.popleft()
            dire.popleft()

        if radiant:
            return "Radiant"
        else:
            return "Dire"

82/82 cases passed (57 ms)
Your runtime beats 66.29 % of python3 submissions
Your memory usage beats 9.62 % of python3 submissions (16.8 MB)

5. AI Solution and Improvements

Your current solution uses two deques to store the indices of the senators from each party. The logic is sound and effective, but it can be slightly improved for readability and Pythonic style. Here are a few suggestions:

  1. Use Python's Naming Conventions: Use snake_case for variable and function names.
  2. Enhance Readability: Add comments to explain the purpose of key steps in the code, enhancing readability for others (or for yourself when you revisit the code later).
  3. Optimize Imports: Import only what you need. If you're only using deque from collections, import it directly.

Here's a revised version of your code with these improvements:

from collections import deque

class Solution:
    def predict_party_victory(self, senate: str) -> str:
        # Queues to store indices of Radiant and Dire senators
        radiant = deque()
        dire = deque()

        # Populate the initial queues with the indices of the senators
        for i, s in enumerate(senate):
            if s == "R":
                radiant.append(i)
            else:
                dire.append(i)

        senate_len = len(senate)

        while radiant and dire:
            # The senator with the lower index bans the other
            if radiant[0] < dire[0]:
                # Radiant senator bans a Dire senator and moves to the end
                radiant.append(radiant[0] + senate_len)
            else:
                # Dire senator bans a Radiant senator and moves to the end
                dire.append(dire[0] + senate_len)
            
            # Remove the senator who exercised their right in this round
            radiant.popleft()
            dire.popleft()

        # Determine the winning party
        return "Radiant" if radiant else "Dire"

This code follows Pythonic standards more closely and is commented for clarity. The logic remains the same, ensuring it works as intended.

profile
Semidragon's network [CS undergrad @ Sungkyunkwan University | Networks + System @ CSI]

0개의 댓글