ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ–ฅ ๋ฌธ์ œ

์•„๋ž˜์™€ ๊ฐ™์ด 5์™€ ์‚ฌ์น™์—ฐ์‚ฐ๋งŒ์œผ๋กœ 12๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
12 = 5 + 5 + (5 / 5) + (5 / 5)
12 = 55 / 5 + 5 / 5
12 = (55 + 5) / 5
5๋ฅผ ์‚ฌ์šฉํ•œ ํšŸ์ˆ˜๋Š” ๊ฐ๊ฐ 6,5,4์ž…๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฒฝ์šฐ๋Š” 4์ž…๋‹ˆ๋‹ค.
์ด์ฒ˜๋Ÿผ ์ˆซ์ž N๊ณผ number๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ, N๊ณผ ์‚ฌ์น™์—ฐ์‚ฐ๋งŒ ์‚ฌ์šฉํ•ด์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ• ์ค‘ N ์‚ฌ์šฉ ํšŸ์ˆ˜์˜ ์ตœ์†Ÿ๊ฐ’์„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

๐Ÿค” ํ’€์ด ๋ฐฉ๋ฒ•

By Mre - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=6342841

์ด ๋ฌธ์ œ๋Š” ๋™์ ๊ณ„ํš๋ฒ•(Dynamic Programming)์œผ๋กœ ๋ถ„๋ฅ˜๋˜์–ด ์žˆ์ง€๋งŒ, ๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰(Depth-First Search)์œผ๋กœ ํ’€์—ˆ๋‹ค.
๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰์ด๋ž€ ๋ฃจํŠธ๋…ธ๋“œ์—์„œ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋‹ค์Œ ๋ธŒ๋žœ์น˜๋กœ ๋„˜์–ด๊ฐ€๊ธฐ ์ „์— ํ•ด๋‹น ๋ธŒ๋žœ์น˜๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

  1. ๋ณ€์ˆ˜
    • countN : N์ด ์‚ฌ์šฉ๋œ ๊ฐœ์ˆ˜
    • resultN : N์„ ์ด์šฉํ•˜์—ฌ ๊ณ„์‚ฐํ•œ ์‹์˜ ๊ฒฐ๊ณผ
    • calN : n ~ nnnnnnnn์˜ ์ˆ˜
  2. if countN > 8 { return }
    • N์€ 1์ด์ƒ 9 ์ดํ•˜์ด๋‹ค. 8์„ ์ดˆ๊ณผํ•˜๋ฉด(0 ~ 8๊นŒ์ง€ ์ด 9๊ฐœ๋ฅผ ์ดˆ๊ณผํ•˜๋ฉด) ๋ฆฌํ„ดํ•ด์ค€๋‹ค. ์ด๋•Œ ๊ฒฐ๊ณผ๋Š” -1์ด ๋œ๋‹ค.
  3. if bumber == resultN : number์™€ ๊ณ„์‚ฐํ•œ ์‹์˜ ๊ฒฐ๊ณผ resultN์ด ๊ฐ™์„ ๊ฒฝ์šฐ
    • result == -1 : ์•„์ง ๊ณ„์‚ฐ๋œ ๊ฒฐ๊ณผ๊ฐ€ ์—†์–ด ์ฒ˜์Œ์œผ๋กœ result๋ฅผ ์—…๋ฐ์ดํŠธ ํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ
    • countN < result : ํ˜„์žฌ result์˜ ๊ฐ’ ๋ณด๋‹ค ๋” ์ ์€ ๊ฐœ์ˆ˜์˜ N์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ
    • ์œ„ ๋‘๊ฐœ์˜ ๊ฒฝ์šฐ์— result๋ฅผ N์ด ์‚ฌ์šฉ๋œ ๊ฐœ์ˆ˜ countN์œผ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๊ณ  ๋ฆฌํ„ดํ•œ๋‹ค.
  4. calN์— n ~ nnnnnnnn์˜ ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋„ฃ์–ด์ฃผ๋ฉฐ ํƒ์ƒ‰ํ•œ๋‹ค.
    for i in 0..<8 { 
     calN = calN * 10 + N
     ...
    }
  5. dfs(N, number, countN + 1 + i, resultN + calN, &result) : resultN์— ๋Œ€ํ•˜์—ฌ calN์„ +, -, *, / ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
    • countN + 1 + i : countN์— N์ด ์‚ฌ์šฉ๋œ ๊ฐœ์ˆ˜๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.
    • resultN + calN : resultN์— ๊ณ„์‚ฐํ•œ ์‹์˜ ๊ฒฐ๊ณผ๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป Swift Code

import Foundation

func solution(_ N:Int, _ number:Int) -> Int {
    var result: Int = -1
    dfs(N, number, 0, 0, &result)
    return result
}

func dfs(_ N:Int, _ number:Int, _ countN:Int, _ resultN:Int, _ result: inout Int) {
    var calN = 0

    if countN > 8 {
        return
    }

    if number == resultN && (result == -1 || countN < result) {
        result = countN
        return
    }

    for i in 0..<8 {
        calN = calN * 10 + N
        dfs(N, number, countN + 1 + i, resultN + calN, &result)
        dfs(N, number, countN + 1 + i, resultN - calN, &result)
        dfs(N, number, countN + 1 + i, resultN * calN, &result)
        dfs(N, number, countN + 1 + i, resultN / calN, &result)
    }
}

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - N์œผ๋กœ ํ‘œํ˜„

 

programmers.co.kr

 

๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
๊ธ€ ๋ณด๊ด€ํ•จ