LeetCode 649. Dota2 Senate

2026. 1. 10. 21:07·코딩테스트/LeetCode

649. Dota2 Senate

 

Problem


 

Solution

The key requirements of this problem are as follows:

  1. The senate is represesnted as a string consisting of R and D, which represent Radiant and Dire.
  2. Each senator can ban one opponent senator in order.
  3. The process continues until only one group remains.
  4. Return the group that finally left.

To satisfy these requirements, I used queue-based approach without importing any external libraries.

 

First, I separate the senators into two lists, one for Radiant and one for Dire.

 

Using enumerate, I store the index of each senator in the list.

After storing all senators, I initialize idx as 0 and start comparing the senators.

 

While idx is smaller than the length of one of the lists, I compare r[idx] and d[idx].

 

If r[idx] is smaller, this means the Radiant senator acts first and ban the Dire senator.

Therefore, the Radiant senator will appear again in the next round, so I append r[idx] + len(senate) ro the Radiant list.

 

If d[idx] is smaller, the same process is applied to the Dire list.

After each comparison, increase idx by 1.

 

When idx reaches the length of one of the lists, this means that group has no more senators to compare.

 

Therefore, the group with the larger list survives and is returned as the final result.

 

class Solution(object):
    def predictPartyVictory(self, senate):
        r = []
        d = []

        for i, j in enumerate(senate):
            if j == "R":
                r.append(i)
            else:
                d.append(i)

        idx = 0
        while idx < len(r) and idx < len(d):
            if r[idx] < d[idx]:
                r.append(r[idx] + len(senate))
            else:
                d.append(d[idx] + len(senate))
            idx += 1

        return "Radiant" if len(r) > len(d) else "Dire"

 


 

https://github.com/K-MarkLee/LeetCode-Practice

 

GitHub - K-MarkLee/LeetCode-Practice: A collection of LeetCode questions to ace the coding interview! - Created using [LeetHub 2

A collection of LeetCode questions to ace the coding interview! - Created using [LeetHub 2.0](https://github.com/maitreya2954/LeetHub-2.0-Firefox) - K-MarkLee/LeetCode-Practice

github.com

 

 

저작자표시 비영리 변경금지 (새창열림)

'코딩테스트 > LeetCode' 카테고리의 다른 글

LeetCode 328. Odd Even Linked List  (0) 2026.01.21
LeetCode 2095. Delete the Middle Node of a Linked List  (0) 2026.01.17
LeetCode 933. Number of Recent Calls  (0) 2026.01.09
LeetCode 394. Decode String  (0) 2026.01.07
LeetCode 2352. Equal Row and Column Pairs  (0) 2026.01.05
'코딩테스트/LeetCode' 카테고리의 다른 글
  • LeetCode 328. Odd Even Linked List
  • LeetCode 2095. Delete the Middle Node of a Linked List
  • LeetCode 933. Number of Recent Calls
  • LeetCode 394. Decode String
코드 유랑자 승열
코드 유랑자 승열
코드 유랑자 승열의 프로그래밍 일지를 남기는 공간입니다.
  • 코드 유랑자 승열
    승열의 프로그래밍 시네마
    코드 유랑자 승열
  • 전체
    오늘
    어제
  • 링크

    • 깃허브 보러가기
    • 링크드인 보러가기
    • 카테고리
      • 코딩테스트
        • LeetCode
        • BaekJoon
      • TIL and WIL
        • TIL
        • WIL
      • 주말스터디
      • 내일배움캠프
        • 사전캠프 강의 (SQL)
      • 용어정리
        • Python
        • Python-Library
        • Machine-Learning
        • Deep-Learning
        • AI 활용
        • LLM & RAG
        • Docker
        • Django
        • SQL
        • Java Script
        • etc
      • Daily 코드카타
        • SQL
        • Python 알고리즘
      • 임시저장
      • 보류
  • 태그

    vector db
    오블완
    django
    RAG
    티스토리챌린지
    word2vec
    langchain
    llm
    template
    View
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
코드 유랑자 승열
LeetCode 649. Dota2 Senate
상단으로

티스토리툴바