์„ฑ๊ฒฉ์œ ํ˜• ๊ฒ€์‚ฌํ•˜๊ธฐ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

2022. 10. 5. 14:16ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

์„ฑ๊ฒฉ์œ ํ˜• ๊ฒ€์‚ฌํ•˜๊ธฐ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

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

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

programmers.co.kr

 

 

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

 

๋”•์…”๋„ˆ๋ฆฌ๋กœ ์นด์šดํŠธ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ ๋ฌธ์ž์—ด์„ ๋งŒ๋“œ๋Š” ๋ฌธ์ œ์ด๋‹ค.

์„ฑ๊ฒฉ์œ ํ˜•์€ ์ง€ํ‘œ๋‹น ๋‘ ๊ฐ€์ง€ ์œ ํ˜• ์ค‘ ํ•˜๋‚˜์ด๋ฏ€๋กœ ๊ฐ๊ฐ ์ง€ํ‘œ์— ๋Œ€ํ•ด ๋ฐฐ์—ด์„ ๋งŒ๋“ ํ›„ sortํ–ˆ๋Š”๋ฐ ํ•ด์‹œ๊ฐ’ ๋”•์…”๋„ˆ๋ฆฌ ์ ‘๊ทผ * sort ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๊ฝค ๋งŽ์ด ๋‚˜์™€์„œ ๊ทธ๋Ÿฐ์ง€ 20๊ฐœ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ค‘ 1๊ฐœ๊ฐ€ ์‹คํŒจํ–ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ sortํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋‹จ์ˆœ ๋น„๊ต๋ฅผ ํ•ด์„œ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์—ˆ๋‹ค.

 

import Foundation

func solution(_ survey:[String], _ choices:[Int]) -> String {
    
    var dic = ["R": 0, "T": 0, "C": 0, "F": 0, "J": 0, "M": 0, "A": 0, "N": 0]
    var idx = 0
    
    for sur in survey {
        switch choices[idx] {
            case 1:
            dic[String(sur[sur.startIndex])]! += 3
            case 2:
            dic[String(sur[sur.startIndex])]! += 2
            case 3:
            dic[String(sur[sur.startIndex])]! += 1
            case 5:
            dic[String(sur[sur.index(after: sur.startIndex)])]! += 1
            case 6:
            dic[String(sur[sur.index(after: sur.startIndex)])]! += 2
            case 7:
            dic[String(sur[sur.index(after: sur.startIndex)])]! += 3
            default:
            break
        }
        idx += 1
    }
    
    let RT = dic["R"]! >= dic["T"]! ? "R" : "T"
    let CF = dic["C"]! >= dic["F"]! ? "C" : "F"
    let JM = dic["J"]! >= dic["M"]! ? "J" : "M"
    let AN = dic["A"]! >= dic["N"]! ? "A" : "N"
    return RT+CF+JM+AN
}

 

 

 

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

 

String์„ Array๋กœ ๋ฐ”๊พธ๋ฉด [Character]๋กœ ๋˜๋Š”๋ฐ ์ด๊ฒƒ์„ ๊ฐ„๊ณผํ•˜๊ณ  ๋ฌธ์ž์—ด ์ธ๋ฑ์Šค๋กœ ํ’€์—ˆ์—ˆ๋‹ค.

๋ฌธ์ œ ์กฐ๊ฑด์ด 1, 2, 3์ผ๋•Œ 3์ , 2์ , 1์ ์„ ์ฃผ๊ณ  5, 6, 7์ผ๋•Œ 1์ , 2์ , 3์  ์ฃผ๋Š” ๊ฒƒ์„๋ณด๊ณ  ์ค‘์‹ฌ์ธ 4๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฐ๊ฐ 1์ , 2์ , 3์ ์ด ์ฐจ์ด๋‚˜๋Š” ๊ฒƒ์„ ์•Œ๊ณ  abs๋กœ์ง์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅธ ์ ‘๊ทผ์ด์—ˆ๋‹ค. (๋‚œ switch๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ํƒ์ƒ‰ํ•จ)

 

๋งˆ์ง€๋ง‰์œผ๋กœ ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฐ’ ๋‘๊ฐœ๋ฅผ ๋น„๊ตํ•  ๋•Œ๋Š” Sortํ•  ํ•„์š”์—†์ด ๊ฐ„๋‹จํžˆ ์กฐ๊ฑด์„ ์ฃผ๊ณ  ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ํ‰์†Œ ์ˆ™์ง€ํ•ด๋†”์•ผ๊ฒ ๋‹ค. (๊ฐ™๋‹ค๋ฅผ ํ‘œํ˜„ํ•จ์œผ๋กœ์จ ์‚ฌ์ „์ˆœ์œผ๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ)

 

import Foundation

func solution(_ survey:[String], _ choices:[Int]) -> String {
    
    var dic: [Character: Int] = ["R": 0, "T": 0, "C": 0, "F": 0, "J": 0, "M": 0, "A": 0, "N": 0]
    
    for i in 0..<survey.count {
        let chars: [Character] = Array(survey[i])
        if choices[i] < 4 {
            dic[chars[0]]! += abs(4 - choices[i])
        } else {
            dic[chars[1]]! += abs(4 - choices[i])
        }
    }
    let rt = dic["R"]! >= dic["T"]! ? "R" : "T"
    let cf = dic["C"]! >= dic["F"]! ? "C" : "F"
    let jm = dic["J"]! >= dic["M"]! ? "J" : "M"
    let an = dic["A"]! >= dic["N"]! ? "A" : "N"
    return rt+cf+jm+an
 }