DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] N์œผ๋กœ ํ‘œํ˜„ (Python) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] N์œผ๋กœ ํ‘œํ˜„ (Python)

gimewn 2022. 4. 26. 15:56

link ๐Ÿ”— https://programmers.co.kr/learn/courses/30/lessons/42895

 

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

 

programmers.co.kr

 

def solution(N, number):
    if N == number:
        return 1
    dp = [set() for _ in range(9)]
    for i in range(1, 9):
        dp[i].add(int(str(N)*i))
        for j in range(1, i):
            for y in dp[j]:
                for x in dp[i-j]:
                    dp[i].add(y+x)
                    dp[i].add(y-x)
                    dp[i].add(y*x)
                    if x != 0:
                        dp[i].add(y//x)
        if number in dp[i]:
            return i
        dp.append(dp[i])
    return -1

 

โœ๏ธ ๋ฉ”๋ชจ

  • ์ค‘๋ณต์„ ์œ„ํ•ด set ์‚ฌ์šฉ
  • set์€ add, ์ธ๋ฑ์‹ฑ ๋ถˆ๊ฐ€๋Šฅ