๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

๋‚œ์ด๋„: level3

๋ฌธ์ œ ํ’€์ด

์ด ๋ฌธ์ œ์˜ ํ•ต์‹ฌ์€ ์นด๋“œ๋ฅผ ๋ผ์šด๋“œ๋งˆ๋‹ค ๋ฝ‘์„ ์ƒ๊ฐ์„ ํ•˜์ง€ ์•Š๊ณ  ํ‚ต ํ•ด๋‘๋Š” ๊ฒƒ์ด๋‹ค.

 

keepCards๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ๋ผ์šด๋“œ๋งˆ๋‹ค ์นด๋“œ 2์žฅ์„ ํ‚ตํ•ด๋‘”๋‹ค.

๋‚ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์นด๋“œ๋Š” myCards๋ผ๋Š” ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.

 

1.  myCards์—์„œ ํƒ€๊ฒŸ ๋„˜๋ฒ„๋ฅผ ๋งŒ๋“ ๋‹ค. (์ฝ”์ธ 0๊ฐœ ์†Œ๋ชจ)

2. 1๋ฒˆ์ด ์•ˆ๋˜๋ฉด ์ฝ”์ธ 1๊ฐœ ์ด์ƒ ์žˆ์„ ๋•Œ, myCards์—์„œ 1์žฅ + keepCards์—์„œ 1์žฅ ์จ์„œ ํƒ€๊ฒŸ ๋„˜๋ฒ„๋ฅผ ๋งŒ๋“ ๋‹ค. (์ฝ”์ธ 1๊ฐœ ์†Œ๋ชจ)

3. 2๋ฒˆ์ด ์•ˆ๋˜๋ฉด ์ฝ”์ธ 2๊ฐœ ์ด์ƒ ์žˆ์„ ๋•Œ, keepCards์—์„œ 2์žฅ์„ ์จ์„œ ํƒ€๊ฒŸ ๋„˜๋ฒ„๋ฅผ ๋งŒ๋“ ๋‹ค. (์ฝ”์ธ 2๊ฐœ ์†Œ๋ชจ)

 

๋งŒ์•ฝ 1,2,3์—์„œ ํƒ€๊ฒŸ ๋„˜๋ฒ„๋ฅผ ๋ชป์ฐพ๋Š”๋‹ค๋ฉด ๋ผ์šด๋“œ ์ข…๋ฃŒ

์ฝ”์ธ๋„ ์—†๊ณ  ๋‚จ์•„์žˆ๋Š” ์นด๋“œ๋„ ์—†๋‹ค๋ฉด ์ข…๋ฃŒ

 

์˜ˆ์ œ๋กœ ์„ค๋ช…ํ•˜์ž๋ฉด

 

์ฝ”์ธ = 4๊ฐœ, ์นด๋“œ ์ˆœ์„œ๋Š” [3, 6, 7, 2, 1, 10, 5, 9, 8, 12, 11, 4]๊ฐ€ ์žˆ๋‹ค.

 

<1๋ผ์šด๋“œ>

myCards = [3, 6, 7, 2]

keepCards = [1, 10] (1, 10 ์ƒˆ ์นด๋“œ ๋“ค์–ด์˜ด)

์ฝ”์ธ = 4๊ฐœ

 

myCards์—์„œ 2์žฅ ๋ฝ‘๊ธฐ -> 6+7=13, ์ฝ”์ธ ์†Œ๋ชจ 0๊ฐœ

 

<2๋ผ์šด๋“œ>

myCards = [3, 2]

keepCards = [1, 10, 5, 9] (5, 9 ์ƒˆ ์นด๋“œ ๋“ค์–ด์˜ด)

์ฝ”์ธ = 4๊ฐœ

 

myCards์—์„œ 2์žฅ ๋ฝ‘๊ธฐ -> ํƒ€๊ฒŸ๋„˜๋ฒ„ ๋งŒ๋“ค ์ˆ˜x

myCards์—์„œ 1์žฅ, keepCards์—์„œ 1์žฅ ๋ฝ‘๊ธฐ -> 3+10=13, ์ฝ”์ธ ์†Œ๋ชจ 1๊ฐœ

 

 

<3๋ผ์šด๋“œ>

myCards = [2]

keepCards = [1, 5, 9, 8, 12] (8, 12 ์ƒˆ ์นด๋“œ ๋“ค์–ด์˜ด)

์ฝ”์ธ = 3๊ฐœ

 

myCards์—์„œ 2์žฅ ๋ฝ‘๊ธฐ -> ํƒ€๊ฒŸ๋„˜๋ฒ„ ๋งŒ๋“ค ์ˆ˜x

myCards์—์„œ 1์žฅ, keepCards์—์„œ 1์žฅ -> ํƒ€๊ฒŸ๋„˜๋ฒ„ ๋งŒ๋“ค ์ˆ˜x

keepCards์—์„œ 2์žฅ ๋ฝ‘๊ธฐ -> 1+12 = 13, ์ฝ”์ธ ์†Œ๋ชจ 2๊ฐœ

 

 

<4๋ผ์šด๋“œ>

myCards = [2]

keepCards = [5, 9, 8, 11, 4] (11, 4 ์ƒˆ ์นด๋“œ ๋“ค์–ด์˜ด)

์ฝ”์ธ = 1๊ฐœ

 

myCards์—์„œ 2์žฅ ๋ฝ‘๊ธฐ -> ํƒ€๊ฒŸ๋„˜๋ฒ„ ๋งŒ๋“ค ์ˆ˜x

myCards์—์„œ 1์žฅ, keepCards์—์„œ 1์žฅ -> 2+11 = 13, ์ฝ”์ธ ์†Œ๋ชจ 1๊ฐœ

 

 

<5๋ผ์šด๋“œ>

myCards = []

keepCards = [5, 9, 8, 4]

์ฝ”์ธ = 0๊ฐœ

 

์ฝ”์ธ๋„ ์—†๊ณ , ๋‚ด ์นด๋“œ๋„ ์—†์Œ -> ๋ผ์šด๋“œ ์ข…๋ฃŒ

 

 

์ฝ”๋“œ

func solution(_ coin:Int, _ cards:[Int]) -> Int {
    var coin = coin
    let n = cards.count
    var point = n/3
    var myCards = cards[0..<n/3].map {Int(String($0))!} // n/3๊ฐœ ๊ฐ€์ง€๊ณ  ์‹œ์ž‘
    var keepCards: [Int] = []
    var round = 1
    let target = n+1
    
myLoop:
    while true {
        // ์ฝ”์ธ๋„ ์—†๊ณ  ๋‚ผ ์ˆ˜ ์žˆ๋Š” ์นด๋“œ๋„ ์—†์œผ๋ฉด
        if (myCards.isEmpty && coin <= 1) || (point == n) {
            break
        }
        let card1 = cards[point]
        let card2 = cards[point+1]
        keepCards.append(card1)
        keepCards.append(card2)

        for i in 0..<myCards.count-1 {
            for j in i+1..<myCards.count {
                let myCard1 = myCards[i]
                let myCard2 = myCards[j]
                // ๋‚ด ์นด๋“œ์—์„œ 2์žฅ์„ ๋ฝ‘์„ ์ˆ˜ ์žˆ์œผ๋ฉด
                if (myCard1+myCard2) == target {
                    round += 1
                    point += 2
                    myCards = myCards.filter {$0 != myCard1 && $0 != myCard2}
                    continue myLoop
                }
            }
        }
       
        // ์ฝ”์ธ์ด 1๊ฐœ ์ด์ƒ ๋‚จ์•„์žˆ๊ณ  ๋‚ด์นด๋“œ+ํ‚ตํ•ด๋‘” ์นด๋“œ๋ฅผ ๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉด
        if coin >= 1 {
            for i in 0..<myCards.count {
                for j in 0..<keepCards.count {
                    let myCard = myCards[i]
                    let keepCard = keepCards[j]
                    
                    // ๋‚ด์นด๋“œ + ํ‚ต์นด๋“œ์จ์„œ ์ฝ”์ธ 1๊ฐœ ์†Œ๋ชจ
                    if myCard+keepCard == target {
                        round += 1
                        coin -= 1
                        point += 2
                        myCards.remove(at: i)
                        keepCards.remove(at: j)
                        continue myLoop
                    }
                }
            }
        }
        
        // ์ฝ”์ธ์ด 2๊ฐœ์ด์ƒ ๋‚จ์•„์žˆ๊ณ  ํ‚ตํ•ด๋‘” ์นด๋“œ์—์„œ ๋‹ค ๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉด
        if coin >= 2 {
            for i in 0..<keepCards.count-1 {
                for j in i+1..<keepCards.count {
                    let keepCard1 = keepCards[i]
                    let keepCard2 = keepCards[j]
                    
                    // ํ‚ต์นด๋“œ 2๊ฐœ ์จ์„œ ์ฝ”์ธ 2๊ฐœ ์†Œ๋ชจ
                    if (keepCard1+keepCard2) == target {
                        round += 1
                        coin -= 2
                        point += 2
                        keepCards = keepCards.filter {$0 != keepCard1 && $0 != keepCard2}
                        continue myLoop
                    }
                }
            }
        }
        break
    }
    return round
}

 

 

ํ›„๊ธฐ

์‹ค์ œ๋กœ ์นด์นด์˜ค ๊ฒจ์šธ ์ธํ„ฐ์‹ญ ์ฝ”ํ…Œ๋ฅผ ๋ดค์—ˆ๋Š”๋ฐ ๊ทธ ๋‹น์‹œ ์ด ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ๋ชปํ–ˆ๋‹ค. (์•„์ด๋””์–ด๊ฐ€ ์•ˆ๋– ์˜ฌ๋ผ ์†๋„ ์•ˆ๋Œ”๋˜ ๊ธฐ์–ต์ด..ใ…‹ใ…‹)

๋‹ค์‹œ ํ’€ ๋•Œ, ํžŒํŠธ๋ฅผ ์กฐ๊ธˆ ๋ณด๋‹ˆ '์นด๋“œ๋ฅผ ํ‚ตํ•ด๋‘”๋‹ค.' ๋ฅผ ์ƒ๊ฐํ•ด๋‚ด๋ฉด ๊ธˆ๋ฐฉ ํ‘ธ๋Š” ๋ฌธ์ œ์˜€๋‹ค. 

๊ณ„์† ๊ทธ๋•Œ ๊ทธ๋•Œ ์ตœ์ ์˜ ์นด๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฝ‘์ง€..? ๋ผ๋Š”๊ฑฐ์— ๋น ์ ธ์„œ ์ƒ๊ฐ๋„ ๋ชปํ–ˆ๋‹ค.

์•„๋งˆ ํžŒํŠธ๋ฅผ ์•ˆ๋ดค์œผ๋ฉด ํ‰์ƒ ๋ชปํ’€์ง€ ์•Š์•˜์„๊นŒ..ใ…Žใ…Ž ์–ด๋ ต๋‹ค~

profile

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป iOSun

@iosun

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

profile on loading

Loading...