[Programmers] N์œผ๋กœ ํ‘œํ˜„*****

5569 ๋‹จ์–ด algorithmpythonalgorithm

๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ

def solution(N, number):
    S = []
    for i in range(1,9):
        S.append(int(str(N)*i))
    result = [{} for _ in range(10)]
    result[1] = {S[0]}
    if N == number:
        return 1
    for j in range(2,9):
        result[j] = {S[j-1]}
        for a in range(j):
            for x in result[a]:
                for y in result[j-a]:
                    result[j].add(x+y)
                    result[j].add(x-y)
                    result[j].add(x*y)
                    if y != 0:
                        result[j].add(x//y)
        if number in result[j]:
            return j
    return -1

ํžŒํŠธ๋ฅผ ์ผ๋‹ค. ์–ด๋ ต๋‹ค.
x/y ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ๊ณผ x//y ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์— ํฐ ์‹œ๊ฐ„ ์ฐจ์ด๊ฐ€ ์žˆ์—ˆ๋‹ค. number๋Š” ์ž์—ฐ์ˆ˜๋กœ ์ฃผ์–ด์ฃผ๊ธฐ์— x//y ๋ฅผ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„๋„ ๊ดœ์ฐฎ์•˜๋‹ค.

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ