๐Ÿ“ ์ฝ”ํ…Œ/HackerRank

[๊ตฌํ˜„] Climbing the Leaderboard

JerryiOS 2023. 5. 15. 20:16

https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem?isFullScreen=true 

 

Climbing the Leaderboard | HackerRank

Help Alice track her progress toward the top of the leaderboard!

www.hackerrank.com

func climbingLeaderboard(ranked: [Int], player: [Int]) -> [Int] {
    var res = Array(repeating: 0, count: player.count)
    var uniqueRanked = [Int]()
    
    for score in ranked {
        if let lastScore = uniqueRanked.last, lastScore == score {
            continue
        }
        uniqueRanked.append(score)
    }
    
    var i = uniqueRanked.count - 1
    for j in 0..<player.count {
        while i >= 0 {
            if player[j] >= uniqueRanked[i] {
                i -= 1
            } else {
                break
            }
        }
        res[j] = i + 2
    }
    
    return res
}

ranked ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ uniqueRanked ๋ฐฐ์—ด์„ ์–ป์–ด๋‚ธ ๋‹ค์Œ,

player ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ
uniqueRanked ๋ฐฐ์—ด์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ถ€ํ„ฐ ์—ญ์œผ๋กœ ์ˆœํšŒํ•œ๋‹ค.

์ด์ œ while๋ฌธ์„ ์ˆœํšŒํ•˜๋ฉด์„œ player๊ฐ€ ๋ช‡๋“ฑ์ธ์ง€ ์ฐพ์„๊ฑฐ๋‹ค.

๋งŒ์•ฝ player์˜ ์ ์ˆ˜๊ฐ€ uniqueRank์— ์ €์žฅ๋œ ์ ์ˆ˜๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด i์˜ ๊ฐ’์„ ๋นผ์ค€๋‹ค. (๋ช‡๋“ฑ์ธ์ง€ ์•Œ๊ธฐ ์œ„ํ•ด)
i+2๊ฐ’์ด player์˜ ๋“ฑ์ˆ˜๊ฐ€ ๋œ๋‹ค. (i+1 : uniqueRank์— ์ €์žฅ๋œ ์„ ์ˆ˜์˜ ๋“ฑ์ˆ˜) 

๋ฐ˜์‘ํ˜•