๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 6์ฃผ์ฐจ ๋ณต์„œ ์ •๋ ฌํ•˜๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 9. 6.
728x90
๋ฐ˜์‘ํ˜•

 

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 6์ฃผ์ฐจ

๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ๋ชธ๋ฌด๊ฒŒ weights์™€, ๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ์ „์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” head2head๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ณต์„œ ์„ ์ˆ˜๋“ค์˜ ๋ฒˆํ˜ธ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ˆœ์„œ๋กœ ์ •๋ ฌํ•œ ํ›„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”

programmers.co.kr


Solution

 

1. ๋ณต์„œ๊ฐ€ ์ด๊ธฐ๊ณ  ์ง„ ์ „์ ๊ณผ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋” ๋งŽ์ด ๋‚˜๊ฐ€๋Š” ์„ ์ˆ˜๋ฅผ ์ด๊ธด ํšŸ์ˆ˜๋ฅผ ์„ธ์ค€๋‹ค.

 

N์€ ์‹ธ์šฐ์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ์ด๋ฏ€๋กœ ์ƒ๋žตํ•ด์ฃผ๊ณ  W์ผ ๊ฒฝ์šฐ๋‚˜ L์ผ ๊ฒฝ์šฐ๋งŒ ์„ธ์ค๋‹ˆ๋‹ค.

 

๊ทธ ์ค‘ ๋งŒ์•ฝ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋” ์ ๋‹ค๋ฉด count๋ฅผ +1 ํ•ด์ค๋‹ˆ๋‹ค.

 

func setFightsHistory(moreWeightsWinCount:inout [Int],_ head2head:[String],weights:[Int]) -> [[String]] {
    var fights:[[String]] = Array(repeating: [String](), count: weights.count)
    for (i,head) in head2head.enumerated() {
       let map = head.map{String($0)}
        for (j,m) in map.enumerated() {
            if m == "W" {
                fights[i].append("W")
                if weights[i] < weights[j] {
                    moreWeightsWinCount[i] += 1
                }
            }
            if m == "L" {
                fights[i].append("L")
            }
        }
    }
    return fights
}

 

2. Boxer ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

 

index,๋ชธ๋ฌด๊ฒŒ,์Šน๋ฅ ,๋ชธ๋ฌด๊ฒŒ๊ฐ€ ๋” ๋งŽ์ด ๋‚˜๊ฐ€๋Š” ์„ ์ˆ˜ ์ด๊ธด ํšŸ์ˆ˜๋ฅผ ์†์„ฑ์œผ๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

struct Boxer {
    let index:Int
    let weight:Int
    let winRate:Double
    let moreWeightsWinCount:Int
}

 

๊ทธ๋ฆฌ๊ณค ๊ฐ ์ •๋ณด๋ฅผ ๋‹ด์•„ Boxer๋ฅผ ๋งŒ๋“ค์–ด์ค๋‹ˆ๋‹ค.

 

index๋Š” ์„ ์ˆ˜๋“ค ๋ฒˆํ˜ธ๊ฐ€ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘์ด๋ฏ€๋กœ +1์„ ํ•ด์ฃผ๊ณ 

 

์Šน๋ฅ ์€ ์‹ธ์šดํšŸ์ˆ˜/์ด๊ธดํšŸ์ˆ˜๋กœ ์„ธํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

func makeBoxers(weights:[Int],fights:[[String]],moreWeightsWinCount:[Int]) -> [Boxer] {
    var boxers:[Boxer] = []
    for (i,w) in weights.enumerated() {
        var winRate:Double = 0
        if fights[i].filter({$0 == "W"}).count > 0 && fights[i].count > 0 {
            winRate = Double(fights[i].filter{$0 == "W"}.count)/Double(fights[i].count)
        }
        let boxer = Boxer(index:i+1,weight: w, winRate: winRate, moreWeightsWinCount: moreWeightsWinCount[i])
        boxers.append(boxer)
    }
    return boxers
}

 

3. ๋ณต์„œ๋“ค์„ ์กฐ๊ฑด์— ๋งž๊ฒŒ ์ •๋ ฌํ•ด์ฃผ๊ณ  index๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.

 

func sortBoxersIndex(boxers:[Boxer]) -> [Int] {
    return boxers.sorted{
        if $0.winRate == $1.winRate {
            if $0.moreWeightsWinCount == $1.moreWeightsWinCount {
                if $0.weight ==  $1.weight {
                    return $0.index < $1.index
                }else {
                    return $0.weight > $1.weight
                }
            }else {
                return $0.moreWeightsWinCount > $1.moreWeightsWinCount
            }
        }else {
            return $0.winRate > $1.winRate
        }
    }.map{$0.index}
}

Source Code

 


P.S

 

1๋‹จ๊ณ„์น˜๊ณค ์ข€ ์–ด๋ ค์› ๋˜ ๋ฌธ์ œ๊ฐ™๋‹ค.. 

 

์ฒ˜์Œ์— ์Šน๋ฅ ์„ ๊ตฌํ•  ๋•Œ 10/์‹ธ์šดํšŸ์ˆ˜ * ์ด๊ธดํšŸ์ˆ˜๋กœ ๊ณ„์‚ฐ์„ ํ–ˆ์—ˆ๋Š”๋ฐ ๊ณ„์† 7๋ฒˆ,9๋ฒˆ,10๋ฒˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๊ฐ€ ํ‹€๋ ธ๋‹ค...

 

๊ทธ๋ƒฅ ์‹ธ์šดํšŸ์ˆ˜/์ด๊ธดํšŸ์ˆ˜๋กœ ํ–ˆ์œผ๋ฉด ๋๋Š”๋ฐ ;;; 

 

๊ทผ๋ฐ ์™œ 10/์‹ธ์šดํšŸ์ˆ˜ * ์ด๊ธดํšŸ์ˆ˜๋กœ ํ•˜๋ฉด ์•ˆ๋˜๋Š”๊ฑฐ์ง€?

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€