์™„์ „ํƒ์ƒ‰ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

2022. 9. 29. 13:09ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

์™„์ „ํƒ์ƒ‰ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://school.programmers.co.kr/learn/courses/30/lessons/42840

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 ๋‚˜์˜ ํ’€์ด

 

cnt ๋ฐฐ์—ด์„ ํŠœํ”Œ๋กœ ๊ตฌ์„ฑํ•ด์„œ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•ด ์ฆ๊ฐ€์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 

if๋ฌธ์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ณ  ์ข€ ๋ณต์žกํ•˜๊ฒŒ ์ธ๋ฑ์Šค์— ์ ‘๊ทผํ•œ ๊ฒƒ ๊ฐ™์•„์„œ ํ’€๊ณ  ๋‚˜์„œ๋„ ๊ต‰์žฅํžˆ ์ฐ์ฐํ–ˆ๋‹ค..

 

import Foundation

func solution(_ answers:[Int]) -> [Int] {
    let arrs = [
        (1, [1, 2, 3, 4, 5]),
        (2, [2, 1, 2, 3, 2, 4, 2, 5]),
        (3, [3, 3, 1, 1, 2, 2, 4, 4, 5, 5])
    ]
    
    var idx = 0
    var cnts = [(Int, Int)](repeating: (0, 0), count: 4)
    
    for i in 1..<4 {
        cnts[i].0 = i
    }
    for arr in arrs {
        answers.forEach { (num) in
            if num == arr.1[idx] {
                cnts[arr.0].1 += 1
            }
            idx += 1
            if idx == arr.1.count {
                idx = 0
            }
        }
        idx = 0
    }
    cnts.remove(at: 0)
    var max = 0
    cnts.forEach { (cnt) in
        if cnt.1 > max {
            max = cnt.1    
        }   
    }
    
    var fillArr = cnts.filter{ $0.1 == max }
    
    if fillArr.count == 1{
        return [fillArr[0].0]
    } else {
        return fillArr.sorted(by: {$0.0 < $1.0}).map{$0.0}
    }
}

 

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•œ ๊น”๋”ํ•œ ํ’€์ด์ด๋‹ค.

๋ฐฐ์—ด์•ˆ์— ํŠœํ”Œ์„ ๋„ฃ์—ˆ๋˜ ๋‚˜์™€ ๋‹ฌ๋ฆฌ ์ด๋ฆ„์žˆ๋Š” ํŠœํ”Œ์— ๋ฐฐ์—ด์„ ๋„ฃ๋Š” ๊ตฌ์กฐ์ด๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ฐฐ์—ด์˜ ๊ฐœ์ˆ˜๋งŒํผ for๋ฌธ์„ ๋„๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋ฌธ์ œ๋ฅผ ํƒ์ƒ‰ํ• ๋•Œ๋งˆ๋‹ค ๊ฐ๊ฐ์— if๋ฌธ์„ ์ฃผ์–ด ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒŒ ์‹ ๊ธฐํ–ˆ๋‹ค.

cnt๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•  ๋•Œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ต‰์žฅํžˆ ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค๊ณ  ๋Š๊ผˆ๋‹ค.

 

import Foundation

func solution(_ answers:[Int]) -> [Int] {
    let answer = (
        a: [1, 2, 3, 4, 5],
        b: [2, 1, 2, 3, 2, 4, 2, 5],
        c: [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
    )
    
    var points = [1:0, 2:0, 3:0]
    
    for (idx, val) in answers.enumerated() {
        if val == answer.a[idx % 5] { points[1]! += 1}
        if val == answer.b[idx % 8] { points[2]! += 1}
        if val == answer.c[idx % 10] { points[3]! += 1}
    }
    
    return points.sorted(by: {$0.key < $1.key}).filter{ points.values.max() == $0.value }.map{$0.key}
}

 

 

 ํ”ผ๋“œ๋ฐฑ

 

1. ๋ญ”๊ฐ€ ๋ฌธ์ œ๊ฐ€ ๋ณต์žกํ•˜๋‹ค๊ณ  ๋Š๋‚„ ๋•Œ๋Š” ์ƒˆ๋กœ์šด ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ๋…ธ๋ ฅํ•ด๋ณด์ž.

2. ์ธ๋ฑ์Šค๊ฐ€ ์ดˆ๊ณผ๋˜๊ณ  ์›์ ์œผ๋กœ ๋Œ์•„์˜ฌ ๋•Œ๋Š” ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ‘ธ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. 

3. ๋”•์…”๋„ˆ๋ฆฌ.values.max()๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด value์— ํ•ด๋‹นํ•˜๋Š” max๊ฐ’์ด ์ถ”์ถœ๋œ๋‹ค. 

4. ๋”•์…”๋„ˆ๋ฆฌ์—์„œ ๊ฐ’[key]๋กœ ์ ‘๊ทผํ•˜๋ฉด ์˜ต์…”๋„์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์–ธ๋ž˜ํ•‘ํ•œ ์ƒํƒœ๋กœ ๊ตฌํ˜„ํ•ด์•ผํ•œ๋‹ค.