antop-dev / algorithm

알고리즘 풀이
MIT License
0 stars 0 forks source link

649. Dota2 Senate #579

Closed antop-dev closed 1 month ago

antop-dev commented 2 months ago

https://leetcode.com/problems/dota2-senate/

antop-dev commented 1 month ago

쉽지 않았다...

image

import java.util.*

class Solution {
    fun predictPartyVictory(senate: String): String {
        val n = senate.length
        val rq = LinkedList<Int>()
        val dq = LinkedList<Int>()
        senate.forEachIndexed { i, c ->
            when (c) {
                'R' -> rq += i
                'D' -> dq += i
            }
        }
        while (rq.isNotEmpty() && dq.isNotEmpty()) {
            val i = minOf(rq.peek(), dq.peek())
            if (i == rq.peek()) {
                dq.poll()
                rq += rq.poll() + n
            } else if (i == dq.peek()) {
                rq.poll()
                dq += dq.poll() + n
            }
        }
        return if (rq.size > dq.size) "Radiant" else "Dire"
    }
}
image