leetcode: 506. Relative Ranks

kldaji·2021년 12월 18일
1

leetcode

목록 보기
15/56

문제링크

풀이1

class Solution {
    fun findRelativeRanks(score: IntArray): Array<String> {
        val answer = Array(score.size) { "" }
        // max heap
        val pq = PriorityQueue<Pair<Int, Int>>( { a, b -> b.first - a.first })
        score.forEachIndexed { index, it ->
            pq.offer(Pair(it, index))
        }
        repeat(score.size) { it ->
            val temp = pq.poll()
            when (it + 1) {
                1 -> answer[temp.second] = "Gold Medal"
                2 -> answer[temp.second] = "Silver Medal"
                3 -> answer[temp.second] = "Bronze Medal"
                else -> answer[temp.second] = (it + 1).toString()
            }
        }
        return answer
    }
}
profile
다양한 관점에서 다양한 방법으로 문제 해결을 지향하는 안드로이드 개발자 입니다.

0개의 댓글