😎풀이

  1. senate에 따라 각 진영의 원로원에게 인덱스를 붙여 구분
  2. 의회를 순회하며 한 진영이 모두 제거될 때까지 앞선 의원이 가장 가까운 상대방 의원 제거
  3. 최종적으로 남은 의원이 존재하는 당의 승리가 됨3. 최종적으로 남은 의원이 존재하는 당의 승리가 됨
function predictPartyVictory(senate: string): string {
    const n = senate.length
    const radiants = []
    const dires = []
    for(let i = 0; i < senate.length; i++) {
        if(senate[i] === 'R') {
            radiants.push(i)
        } else {
            dires.push(i)
        }
    }
    while(radiants.length && dires.length) {
        const radiant = radiants.shift()
        const dire = dires.shift()
        if(radiant < dire) {
            radiants.push(radiant + n)
        } else {
            dires.push(dire + n)
        }
    }
    if(dires.length) return 'Dire'
    return 'Radiant'
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글