Problem
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/์ธ์ดํ์ * ์ด๊ธดํ์๋ก ํ๋ฉด ์๋๋๊ฑฐ์ง?
'๐ Problem Solution > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ํ๋ก๊ทธ๋๋จธ์ค ๋ค๋จ๊ณ ์นซ์ ํ๋งค (0) | 2021.09.12 |
---|---|
[Swift] ํ๋ก๊ทธ๋๋จธ์ค ๊ธฐ์ง๊ตญ ์ค์น (0) | 2021.09.10 |
[Swift] 2019 KAKAO BLIND RECRUITMENT ๋งค์นญ ์ ์ (0) | 2021.09.04 |
[Swift] 2020 KAKAO INTERNSHIP ๊ฒฝ์ฃผ๋ก ๊ฑด์ค (0) | 2021.08.31 |
[Swift] ํ๋ก๊ทธ๋๋จธ์ค ์ํด๋ฆฌ ์ฑ๋ฆฐ์ง 5์ฃผ์ฐจ ๋ชจ์ ์ฌ์ (0) | 2021.08.30 |
๋๊ธ