๋กœ๋˜์˜ ์ตœ๊ณ  ์ˆœ์œ„์™€ ์ตœ์ € ์ˆœ์œ„ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

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

๋กœ๋˜์˜ ์ตœ๊ณ  ์ˆœ์œ„์™€ ์ตœ์ € ์ˆœ์œ„ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

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

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

programmers.co.kr

 

 

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

 

0์„ ์–ผ๋งˆ๋‚˜ ๋‹ด๊ณ ์žˆ๋Š”์ง€ ์นด์šดํŠธ๋ฅผ ํ•˜๊ณ  ๋‹น์ฒจ๋ฒˆํ˜ธ๊ฐ€ ๋ช‡๊ฐœ์ธ์ง€๋ฅผ ์นด์šดํŠธํ•ด์„œ switch๋ฌธ์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

import Foundation

func solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] {
    
    var zeroBeforeLotto = win_nums.filter{ lottos.contains($0) }.count
    let zeroCount = lottos.filter{ $0 == 0 }.count
    
    var bestCount = zeroBeforeLotto + zeroCount
    var worstNum = 0
    
    var result:[Int] = []
    switch bestCount {
        case 6: 
        if zeroCount == 6 {
            result.append(1)
            result.append(0+zeroCount)   
        } else {
            result.append(1)
            result.append(1+zeroCount)
        }
        case 5:
        result.append(2)
        result.append(2+zeroCount)
        case 4:
        result.append(3)
        result.append(3+zeroCount)
        case 3:
        result.append(4)
        result.append(4+zeroCount)
        case 2:
        result.append(5)
        result.append(5+zeroCount)
        default:
        result.append(6)
        result.append(6)
    }
    
    return result
}

 

(switch๋ฌธ์˜ ์•ˆ์—์„œ์˜ ํ•˜๋“œ์ฝ”๋”ฉ.. ๋ณด์ด์‹œ๋‚˜์š”..?)

case6์—์„œ if๋ฌธ์„ ๋„ฃ์€ ๊ฒƒ์€ 6๊ฐœ๊ฐ€ 0์ผ๋•Œ  1+6์„ ํ•˜๊ฒŒ๋˜๋ฉด 7์œ„๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

์ด๋Ÿฐ ์ ˆ๋ง์ ์ธ ํ’€์ด ์†์—์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  ๋‚ด ํ’€์ด๋ฅผ ๊ฐœ์„ ํ–ˆ๋‹ค! 

 

 

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

 

import Foundation

func solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] {
    
    var winCount = win_nums.filter{ lottos.contains($0) }.count
    let zeroCount = lottos.filter{ $0 == 0 }.count
    
    return [min(7-winCount-zeroCount, 6), min(7-winCount, 6)]
}

 

์•„๋ฆ„๋‹ค์šด ํ’€์ด๋‹ค. ๋‹น์ฒจ๊ฐœ์ˆ˜์— ๋”ฐ๋ผ์„œ ์ˆœ์œ„๊ฐ€ ๋ณ€๋™๋˜๋Š” ๊ฒƒ์„ 7-winCount๋กœ ํ‘œํ˜„ํ–ˆ๊ณ (๋‹น์ฒจ๋œ ๋ฒˆํ˜ธ์˜ ๊ฐœ์ˆ˜๊ฐ€ 5๊ฐœ๋ผ๋ฉด 2๋“ฑ์ด๊ธฐ ๋•Œ๋ฌธ), ๋‹น์ฒจ๊ฐœ์ˆ˜๋„ ์—†๊ณ  0์˜ ๊ฐœ์ˆ˜๋„ ์—†์„๋•Œ 7์ด๋˜๋Š”๋ฐ ์ด๋ฅผ minํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด 6๊ฐœ๋กœ ์ œํ•œํ•œ ์ ์ด ์ธ์ƒ๊นŠ๋‹ค.

 

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด - Set์ž๋ฃŒ๊ตฌ์กฐ

 

import Foundation

func solution(_ lottos:[Int], _ win_nums:[Int]) -> [Int] {
    
    let mine = Set(lottos)
    let wins = Set(win_nums)
    let score = mine.intersection(wins).count
    let joker = lottos.filter{ $0 == 0 }.count
    let ranking = [6, 6, 5, 4, 3, 2, 1]
    
    return [ranking[score+joker], ranking[score]]
    
}

 

ranking ๋ฐฐ์—ด์„ ํ†ตํ•ด์„œ 1๊ฐœ์ผ๋•Œ์™€ 2๊ฐœ์ผ๋•Œ์˜ ๋“ฑ์ˆ˜๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๊ณ  ์ด๋ฅผ ํ†ตํ•ด ์ง๊ด€์ ์ธ ํ’€์ด๊ฐ€ ๊ฐ€๋Šฅํ–ˆ๋‹ค.

(์—ญ์‹œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ๋ด์•ผ ์ƒ๊ฐ์ด ํ™•์žฅ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค)

 

 

 ํ”ผ๋“œ๋ฐฑ

 

1. ๋“ฑ์ˆ˜๋ฅผ ์ฒดํฌํ•  ๋•Œ 0์œ„๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์— 7์—์„œ ๋นผ์ฃผ๊ณ  minํ•จ์ˆ˜๋กœ ์ตœ์†Œ๊ฐ’์„ ์ •ํ•ด์ฃผ๋Š” ๊ฐœ๋…์„ ์ˆ™์ง€ํ•˜์ž.

2. intersectionํ•จ์ˆ˜๋Š” Set๋ฉ”์„œ๋“œ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.